-
Notifications
You must be signed in to change notification settings - Fork 19
/
10kindsofpeopleNAIVE.java
76 lines (56 loc) · 1.5 KB
/
10kindsofpeopleNAIVE.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
import java.util.*;
public class 10kindsofpeopleNAIVE {
// This solution will give you TLE
static int[] R = {1 , -1 , 0 , 0};
static int[] C = {0 , 0 , 1 , -1};
public static boolean bfs(Point1 start , Point1 end , char[][] map) {
boolean[][] visited = new boolean[map.length][map[0].length];
ArrayList<Point1> queue = new ArrayList<>();
queue.add(start);
visited[start.r][start.c] = true;
while (!queue.isEmpty())
{
Point1 curr = queue.remove(0);
int r = curr.r;
int c = curr.c;
for (int i = 0; i < 4; i++)
{
int tryR = r + R[i];
int tryC = c + C[i];
if (tryR >= 0 && tryR < map.length && tryC >= 0 && tryC < map[0].length)
if (map[tryR][tryC] == map[r][c] && !visited[tryR][tryC])
{
visited[tryR][tryC] = true;
queue.add(new Point1(tryR , tryC));
}
}
}
return visited[end.r][end.c];
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int rows = scan.nextInt();
int columns = scan.nextInt();
char[][] map = new char[rows][columns];
for (int i = 0; i < rows; i++)
map[i] = scan.next().toCharArray();
int queries = scan.nextInt();
for (int i = 0; i < queries; i++)
{
Point1 start = new Point1(scan.nextInt() - 1 , scan.nextInt() - 1);
Point1 end = new Point1(scan.nextInt() - 1 , scan.nextInt() - 1);
if (bfs(start , end , map))
System.out.println( map[start.r][start.c] == '1' ? "decimal" : "binary");
else
System.out.println("neither");
}
scan.close();
}
}
class Point1 {
int r , c;
public Point1 (int a , int b) {
r = a;
c = b;
}
}