-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11581_Grid_Successors.cpp
More file actions
88 lines (66 loc) · 2.02 KB
/
11581_Grid_Successors.cpp
File metadata and controls
88 lines (66 loc) · 2.02 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
83
84
85
86
87
88
#include <bits/stdc++.h>
#define ALL(x) x.begin(), x.end()
#define FAST std::cin.tie(0); ios::sync_with_stdio(false); std::cout.tie(0);
using namespace std;
#define min(a,b) (a < b) ? (a) : (b)
#define max(a,b) (a > b) ? (a) : (b)
#define vii vector<pair<int,int>>
#define ii pair<int, int>
#define ll long long
#define TC int t; cin >> t; while (t--)
int main(int argc, char const *argv[])
{
int tc;
cin >> tc;
int g[3][3];
string line;
while (tc--) {
getline(cin, line);
int zer = 0;
for (int i = 0; i < 3; i++) {
cin >> line;
for (int j = 0; j < 3; j++) {
g[i][j] = line[j] - '0';
if (line[j] == '0') zer++;
}
}
int f[3][3];
int count = -1;
bool go = true;
// if g is all zeros (special case)
if (zer == 9) go = false;
while (go) {
zer = 0;
count++;
for (int i = 0; i < 3 && go; i++) {
for (int j = 0; j < 3 && go; j++) {
f[i][j] = 0;
int s = 0;
if (i+1 < 3) s+= g[i+1][j];
if (i-1 >= 0) s+= g[i-1][j];
if (j+1 < 3) s+= g[i][j+1];
if (j-1 >=0) s+= g[i][j-1];
// if there is a one and all its neighbours are zeros.
// it will always give the same array.
if (s == 0 && f[i][j] == 1) {
count = -1;
go = false;
}
s=s%2;
f[i][j] = s;
if (f[i][j] == 0) zer++;
}
}
// all zeros
if (zer == 9 || !go) break;
// now g is previous f
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
g[i][j] = f[i][j];
}
}
}
cout << count << endl;
}
return 0;
}