-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConway’s Game Of Life.cpp
More file actions
82 lines (76 loc) · 1.94 KB
/
Copy pathConway’s Game Of Life.cpp
File metadata and controls
82 lines (76 loc) · 1.94 KB
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
/*
1
10
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
*/
#include<bits/stdc++.h>
using namespace std;
void conwaysGame(int);
bool isSafe(int, int, int);
int main(){
int t, n;
cin >> t;
while(t--){
cin >> n;
conwaysGame(n);
cout << endl;
}
return 0;
}
void conwaysGame(int n){
int arr[n][n], load[n][n];
int xMove[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int yMove[] = {-1, 0, 1, 1, 1, 0, -1, -1};
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++){
cin >> arr[i][j];
load[i][j] = arr[i][j];
}
int hi = 5;
while(1){
int flag = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++){
int count = 0;
for(int k = 0; k < 8; k++){
int _x = i + xMove[k];
int _y = j + yMove[k];
if(isSafe(_x, _y, n) && arr[_x][_y] == 1)
count++;
}
if(arr[i][j] == 1 && count < 2)
load[i][j] = 0, flag = 1;
else if(arr[i][j] == 1 && count > 3)
load[i][j] = 0, flag = 1;
else if(arr[i][j] == 0 && count == 3)
load[i][j] = 1, flag = 1;
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
arr[i][j] = load[i][j];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
char ch = (arr[i][j] == 1) ? '*' : '.';
cout << ch << " ";
}
cout << endl;
}
cout << "++++++++++++++\n";
if(flag == 0)
break;
}
}
bool isSafe(int x, int y, int n){
if(0 <= x && x < n && 0 <= y && y < n)
return true;
return false;
}