-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10KindsOfPeople.cpp
More file actions
129 lines (116 loc) · 2.63 KB
/
10KindsOfPeople.cpp
File metadata and controls
129 lines (116 loc) · 2.63 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
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
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n, k, t;
int r1,r2,c1,c2;
int count0 = -1;
int count1 = 2;
std::string line;
std::cin >> n >> k;
std::vector<std::vector<int>> field(n, std::vector<int>(k));
// read field
for (int i = 0; i < n; i++) {
std::cin >> line;
for (int j = 0; j < k; j++) {
field[i][j] = line[j]-'0';
}
}
// process field
std::stack<int> iStack, jStack;
int ii, jj;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
if (field[i][j] == 0) {
iStack.push(i);
jStack.push(j);
while(!iStack.empty()) {
ii = iStack.top();
jj = jStack.top();
field[ii][jj] = count0;
if (ii > 0 && field[ii-1][jj] == 0) { //up
iStack.push(ii-1);
jStack.push(jj);
}
else if (jj < k-1 && field[ii][jj+1] == 0) { //right
iStack.push(ii);
jStack.push(jj+1);
}
else if (ii < n-1 && field[ii+1][jj] == 0) { //down
iStack.push(ii+1);
jStack.push(jj);
}
else if (jj > 0 && field[ii][jj-1] == 0) { //left
iStack.push(ii);
jStack.push(jj-1);
}
else {
iStack.pop();
jStack.pop();
}
}
count0--;
} else if (field[i][j] == 1) {
iStack.push(i);
jStack.push(j);
while(!iStack.empty()) {
ii = iStack.top();
jj = jStack.top();
field[ii][jj] = count1;
if (ii > 0 && field[ii-1][jj] == 1) { //up
iStack.push(ii-1);
jStack.push(jj);
}
else if (jj < k-1 && field[ii][jj+1] == 1) { //right
iStack.push(ii);
jStack.push(jj+1);
}
else if (ii < n-1 && field[ii+1][jj] == 1) { //down
iStack.push(ii+1);
jStack.push(jj);
}
else if (jj > 0 && field[ii][jj-1] == 1) { //left
iStack.push(ii);
jStack.push(jj-1);
}
else {
iStack.pop();
jStack.pop();
}
}
count1++;
}
}
}
// read queries and test
std::string output;
std::cin >> t;
for (int i = 0; i < t; i++) {
std::cin >> r1;
std::cin >> c1;
std::cin >> r2;
std::cin >> c2;
if (field[r1-1][c1-1] == field[r2-1][c2-1]) {
output = (field[r1-1][c1-1] < 0 ? "binary" : "decimal");
} else {
output = "neither";
}
std::cout << output << '\n';
}
// debug output
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
std::cout << field[i][j];
if (field[i][j] > 0) {
std::cout << ' ';
}
}
std::cout << "\n";
}*/
return 0;
}