forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10kindsofpeople.cpp
115 lines (97 loc) · 2.32 KB
/
10kindsofpeople.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
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
#include <iostream>
#include <vector>
using namespace std;
int find(vector<int>& disjoint, int a) {
if(disjoint[a] == -1) {
return a;
}
disjoint[a] = find(disjoint, disjoint[a]);
return disjoint[a];
}
void join(vector<int>& disjoint, int a, int b) {
a = find(disjoint, a);
b = find(disjoint, b);
if(a == b) {
return;
}
disjoint[a] = b;
}
bool inrange(int x, int y, int ti, int tj) {
if(ti < 0) {
return false;
}
if(tj < 0) {
return false;
}
if(ti >= x) {
return false;
}
if(tj >= y) {
return false;
}
return true;
}
int main() {
int x, y;
cin >> x >> y;
vector<vector<int>> zones;
zones.resize(x, vector<int>(y));
// Take in map
for(int i = 0; i < x; i++) {
for(int j = 0; j < y; j++) {
char val;
cin >> val;
zones[i][j] = val - '0';
}
}
// Build disjoint set
vector<int> disjoint;
disjoint.resize(x * y, -1);
for(int i = 0; i < x; i++) {
for(int j = 0; j < y; j++) {
int ti, tj;
ti = i-1;
tj = j;
if(inrange(x, y, ti, tj) && zones[i][j] == zones[ti][tj]) {
join(disjoint, i*y+j, ti*y+tj);
}
ti = i;
tj = j-1;
if(inrange(x, y, ti, tj) && zones[i][j] == zones[ti][tj]) {
join(disjoint, i*y+j, ti*y+tj);
}
ti = i+1;
tj = j;
if(inrange(x, y, ti, tj) && zones[i][j] == zones[ti][tj]) {
join(disjoint, i*y+j, ti*y+tj);
}
ti = i;
tj = j+1;
if(inrange(x, y, ti, tj) && zones[i][j] == zones[ti][tj]) {
join(disjoint, i*y+j, ti*y+tj);
}
}
}
// Run all the queries
int queries;
cin >> queries;
while(queries--) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
x2--;
y1--;
y2--;
if(find(disjoint, x1*y+y1) == find(disjoint, x2*y+y2)) {
if(zones[x1][y1] == 1) {
cout << "decimal" << endl;
}
else {
cout << "binary" << endl;
}
}
else {
cout << "neither" << endl;
}
}
}