-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10kindsofpeople.java
110 lines (80 loc) · 2.65 KB
/
10kindsofpeople.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
108
109
110
import java.util.ArrayList;
public class T10kindsofpeoplePRO {
static int[] R = {0,1,0,-1};
static int[] C = {1,0,-1,0};
//WITH A CRUCIAL FIX BY PEDRO CONTIPELLI: https://github.com/PedroContipelli
public static void setgroup(TPnode[][] map, int r, int c, int group){
ArrayList<TPnode> queue = new ArrayList<>();
queue.add(map[r][c]);
map[r][c].group = group;
while (!queue.isEmpty())
{
TPnode n = queue.remove(0);
for (int i = 0; i < 4; i++)
{
int newR = n.r + R[i];
int newC = n.c + C[i];
if (ir(map, newR, newC))
if (map[r][c].val == map[newR][newC].val && map[newR][newC].group == -1){
map[newR][newC].group = group;
queue.add(map[newR][newC]);
}
}
}
}
public static boolean ir(TPnode[][] map, int r, int c){
return r > -1 && c > -1 && r < map.length && c < map[0].length;
}
public static void main(String[] args) {
Kattio scan = new Kattio(System.in);
int rows = scan.getInt();
int cols = scan.getInt();
TPnode[][] map = new TPnode[rows][cols];
for(int r=0; r < rows; r++){
String line = scan.getWord();
for(int c=0; c < cols; c++){
map[r][c] = new TPnode(r, c, line.charAt(c));
}
}
int group = 1;
for(int r=0; r < rows; r++){
for(int c=0; c < cols; c++){
if(map[r][c].group == -1){
setgroup(map, r, c, group);
group++;
}
}
}
int cases = scan.getInt();
while(cases --> 0){
int r1 = scan.getInt()-1;
int c1 = scan.getInt()-1;
int r2 = scan.getInt()-1;
int c2 = scan.getInt()-1;
if(map[r1][c1].group == map[r2][c2].group){
if(map[r1][c1].val == '0')
System.out.println("binary");
else
System.out.println("decimal");
}
else{
System.out.println("neither");
}
}
scan.close();
}
}
class TPnode{
int r;
int c;
char val;
int group = -1;
public TPnode(int r, int c, char val){
this.r = r;
this.c = c;
this.val = val;
}
public String toString(){
return val + " ";
}
}