-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist3.cpp
145 lines (145 loc) · 3.46 KB
/
list3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "lifealgo.h"
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std ;
const int END = 0x7ffffff0 ;
class list3algo : public lifealgo {
public:
virtual void init(int w, int h) ;
virtual void setcell(int x, int y) ;
virtual int getpopulation() ;
virtual int nextstep(int, int, int) ;
virtual void swap() ;
void createit() ;
int created ;
vector<pair<int, int> > pts ;
vector<int> a0, a1 ;
} ;
static class list3algofactory : public lifealgofactory {
public:
list3algofactory() ;
virtual lifealgo *createInstance() {
return new list3algo() ;
}
} factory ;
list3algofactory::list3algofactory() {
registerAlgo("list3", &factory) ;
}
void list3algo::init(int w_, int h_) {
}
void list3algo::setcell(int x, int y) {
created = 0 ;
pts.push_back(make_pair(y, x)) ;
}
int list3algo::getpopulation() {
if (!created)
createit() ;
int r = 0 ;
int at = 0 ;
while (a0[at] != END) { // for each row
at++ ;
while (a0[at] != END) { // for each point
at++ ;
r++ ;
}
at++ ;
}
return r ;
}
void list3algo::swap() { }
void list3algo::createit() {
if (!created) {
sort(pts.begin(), pts.end()) ;
int cury = -1 ;
for (size_t i=0; i<pts.size(); i++) {
if (pts[i].first != cury) {
if (cury != -1)
a0.push_back(END) ;
cury = pts[i].first ;
a0.push_back(cury) ;
}
a0.push_back(pts[i].second) ;
}
a0.push_back(END) ;
if (cury != -1)
a0.push_back(END) ;
created = 1 ;
}
}
int list3algo::nextstep(int id, int nid, int needpop) {
int r = 0 ;
if (nid != 1)
error("! multithreading not yet supported") ;
if (!created)
createit() ;
a1.clear() ;
int r0=0, r1=0, r2=0 ;
int wy = END ;
while (a0[r0] != END) {
int y = min(a0[r0]+1, min(a0[r1], a0[r2]-1)) ;
int p0 = r0+1 ;
int p1 = r1 ;
int p2 = r2 ;
if (a0[p1] != END)
p1++ ;
if (a0[p2] != END)
p2++ ;
int x = -END ;
while (1) {
int xt = END ;
if (a0[r0] == y-1)
xt = a0[p0] ;
if (a0[r1] == y)
xt = min(xt, a0[p1]) ;
if (a0[r2] == y+1)
xt = min(xt, a0[p2]) ;
if (xt == END) {
if (a0[r0] == y-1)
r0 = p0 + 1 ;
if (a0[r1] == y)
r1 = p1 + 1 ;
if (a0[r2] == y+1)
r2 = p2 + 1 ;
break ;
}
x = max(x, xt-1) ;
int n = 0 ;
if (a0[r0] == y-1) {
for (int i=0; a0[p0+i] < x+2; i++)
n++ ;
if (a0[p0] == x-1)
p0++ ;
}
if (a0[r1] == y) {
for (int i=0; a0[p1+i] < x+2; i++)
n++ ;
if (a0[p1] == x-1)
p1++ ;
}
if (a0[r2] == y+1) {
for (int i=0; a0[p2+i] < x+2; i++)
n++ ;
if (a0[p2] == x-1)
p2++ ;
}
if (n == 3 || (n == 4 && a0[r1] == y && a0[p1] == x)) {
if (wy != y) {
if (wy != END)
a1.push_back(END) ;
wy = y ;
a1.push_back(y) ;
}
a1.push_back(x) ;
r++ ;
}
x++ ;
}
}
a1.push_back(END) ;
if (wy != END)
a1.push_back(END) ;
::swap(a0, a1) ;
return r ;
}