-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray4.cpp
61 lines (61 loc) · 1.6 KB
/
array4.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
#include "lifealgo.h"
#include <algorithm>
#include <cstdlib>
using namespace std ;
class array4algo : 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() ;
int w, h ;
long long wh ;
} ;
const int MAXDIMEN = 8192 ;
static unsigned char u0[MAXDIMEN][MAXDIMEN] ;
static unsigned char u1[MAXDIMEN][MAXDIMEN] ;
static class array4algofactory : public lifealgofactory {
public:
array4algofactory() ;
virtual lifealgo *createInstance() {
return new array4algo() ;
}
} factory ;
array4algofactory::array4algofactory() {
registerAlgo("array4", &factory) ;
}
void array4algo::init(int w_, int h_) {
w = w_ ;
h = h_ ;
wh = w * h ;
}
void array4algo::setcell(int x, int y) {
u0[y][x] = 1 ;
}
int array4algo::getpopulation() {
int r = 0 ;
for (int y=1; y+1<h; y++)
for (int x=1; x+1<w; x++)
r += u0[y][x] ;
return r ;
}
void array4algo::swap() {}
int array4algo::nextstep(int id, int nid, int) {
if (nid != 1)
error("! multithreading not supported yet") ;
int pop = 0 ;
for (int i=0; i<=h; i++)
u1[i][0] = 0 ;
for (int j=0; j<=w; j++)
u1[0][j] = 0 ;
for (int i=0; i<h; i++)
for (int j=0; j<w; j++)
u1[i+1][j+1] = u0[i][j] + u1[i][j+1] + u1[i+1][j] - u1[i][j] ;
for (int i=1; i+1<h; i++)
for (int j=1; j+1<w; j++) {
unsigned char n = u1[i+2][j+2] - u1[i+2][j-1] - u1[i-1][j+2] + u1[i-1][j-1] ;
pop += u0[i][j] = (n == 3 || (n == 4 && u0[i][j])) ;
}
return pop ;
}