-
Notifications
You must be signed in to change notification settings - Fork 0
/
conway_2.cpp
77 lines (67 loc) · 2.39 KB
/
conway_2.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
#include <iostream>
#include <utility>
#include <vector>
// N NE E SE S SW W NW
std::vector<int> di = {-1, -1, 0, 1, 1, 1, 0, -1};
std::vector<int> dj = { 0, 1, 1, 1, 0, -1, -1, -1};
std::vector<std::vector<int>> conway(const int &n, const int &m, const std::vector<std::pair<int, int>> &v, const int &k) {
std::vector<std::vector<int>> grid(n + 2, std::vector<int>(m + 2, 0));
for (const std::pair<int, int> &cell : v) {
grid[cell.first + 1][cell.second + 1] = 1;
}
std::vector<std::vector<int>> prev = grid;
for (int gen = 0; gen < k; ++gen) {
prev = grid;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
int cnt = 0;
for (int d = 0; d < 8; ++d) {
int ni = i + di[d];
int nj = j + dj[d];
cnt += prev[ni][nj];
}
// 1. Any live cell with fewer than two live neighbours dies, as if by underpopulation.
if (prev[i][j] == 1 && cnt < 2) {
grid[i][j] = 0;
}
// 2. Any live cell with two or three live neighbours lives on to the next generation.
if (prev[i][j] == 1 && (cnt == 2 || cnt == 3)) {
grid[i][j] = 1;
}
// 3. Any live cell with more than three live neighbours dies, as if by overpopulation.
if (prev[i][j] == 1 && cnt > 3) {
grid[i][j] = 0;
}
// 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
if (prev[i][j] == 0 && cnt == 3) {
grid[i][j] = 1;
}
}
}
}
return grid;
}
int main() {
FILE *fp = freopen("in.txt", "r", stdin);
int n, m;
std::cin >> n >> m;
int p;
std::cin >> p;
std::vector<std::pair<int, int>> v(p);
for (int i = 0; i < p; ++i) {
std::cin >> v[i].first >> v[i].second;
}
int k;
std::cin >> k;
fclose(fp);
std::vector<std::vector<int>> ans = conway(n, m, v, k);
FILE *fp2 = freopen("out2.txt", "w", stdout);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
std::cout << ans[i][j] << " ";
}
std::cout << "\n";
}
fclose(fp2);
return 0;
}