-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtenkindsofpeople.java
107 lines (99 loc) · 3.85 KB
/
tenkindsofpeople.java
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
import java.util.*;
import java.io.*;
class IntegerPair {
public int first,second;
public IntegerPair(int row, int col) {
first = row;
second = col;
}
}
public class tenkindsofpeople {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] inputrc = br.readLine().split(" ");
int r = Integer.parseInt(inputrc[0]);
int c = Integer.parseInt(inputrc[1]);
int[][] map = new int[r][c];
int[][] visited = new int[r][c];
for(int i = 0; i < r; i++) {
String line = br.readLine();
for(int j = 0; j < c; j++) {
map[i][j] = Integer.parseInt(line.substring(j, j + 1));
visited[i][j] = 0;
}
}
int count = 1;
for(int i = 0; i < r; i ++) {
for (int j = 0; j < c; j ++) {
if (map[i][j] == 0) {
if (visited[i][j] == 0) {
visited[i][j] = count;
Queue<IntegerPair> queue = new LinkedList<IntegerPair>();
queue.offer(new IntegerPair(i,j));
while (queue.size() > 0) {
IntegerPair v = queue.poll();
int a = v.first;
int b = v.second;
if (a > 0 && map[a-1][b] == 0 && visited[a-1][b] == 0) {
queue.offer(new IntegerPair(a - 1,b));
visited[a-1][b] = count;
}
if (a < r - 1 && map[a+1][b] == 0 && visited[a+1][b] == 0) {
queue.offer(new IntegerPair(a + 1,b));
visited[a+1][b] = count;
}
if (b > 0 && map[a][b-1] == 0 && visited[a][b-1] == 0) {
queue.offer(new IntegerPair(a,b - 1));
visited[a][b-1] = count;
}
if (b < c - 1 && map[a][b+1] == 0 && visited[a][b+1] == 0) {
queue.offer(new IntegerPair(a,b + 1));
visited[a][b+1] = count;
}
}
count ++;
}
} else {
if (visited[i][j] == 0) {
visited[i][j] = count;
Queue<IntegerPair> queue = new LinkedList<IntegerPair>();
queue.offer(new IntegerPair(i,j));
while (queue.size() > 0) {
IntegerPair v = queue.poll();
int a = v.first;
int b = v.second;
if (a > 0 && map[a-1][b] == 1 && visited[a-1][b] == 0) {
queue.offer(new IntegerPair(a - 1,b));
visited[a-1][b] = count;
}
if (a < r - 1 && map[a+1][b] == 1 && visited[a+1][b] == 0) {
queue.offer(new IntegerPair(a + 1,b));
visited[a+1][b] = count;
}
if (b > 0 && map[a][b-1] == 1 && visited[a][b-1] == 0) {
queue.offer(new IntegerPair(a,b - 1));
visited[a][b-1] = count;
}
if (b < c - 1 && map[a][b+1] == 1 && visited[a][b+1] == 0) {
queue.offer(new IntegerPair(a,b + 1));
visited[a][b+1] = count;
}
}
count ++;
}
}
}
}
int queries = Integer.parseInt(br.readLine());
for (int i = 0; i < queries; i ++) {
String[] input = br.readLine().split(" ");
int r1 = Integer.parseInt(input[0]) - 1;
int c1 = Integer.parseInt(input[1]) - 1;
int r2 = Integer.parseInt(input[2]) - 1;
int c2 = Integer.parseInt(input[3]) - 1;
if (map[r1][c1] == 0 && visited[r1][c1] == visited[r2][c2]) System.out.println("binary");
else if (map[r1][c1] == 1 && visited[r1][c1] == visited[r2][c2]) System.out.println("decimal");
else System.out.println("neither");
}
}
}