Repository navigation
Expand file tree
/
Copy pathSolution1227.java
More file actions
50 lines (47 loc) · 1.58 KB
/
Copy pathSolution1227.java
File metadata and controls
50 lines (47 loc) · 1.58 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
package swea.queue;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Solution1227 {
static int n, m = 100;
static String s;
static char[][] arr;
static int[] dx = { 0, 0, -1, 1 };
static int[] dy = { -1, 1, 0, 0 };
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Queue<int[]> q = new LinkedList<>();
arr = new char[m][m];
for (int i = 0; i < 10; i++) {
n = sc.nextInt();
for (int j = 0; j < m; j++) {
s = sc.next();
for (int k = 0; k < m; k++) {
arr[j][k] = s.charAt(k);
if (arr[j][k] == '2') {
arr[j][k] = '1';
q.add(new int[] { j, k });
}
}
}
boolean flag = false;
while (!q.isEmpty()) {
int[] x = q.poll();
for (int j = 0; j < 4; j++) {
int nx = dx[j] + x[0];
int ny = dy[j] + x[1];
if (0 <= nx && nx < m && 0 <= ny && ny < m && arr[nx][ny] != '1') {
if (arr[nx][ny] == '3') {
flag = true;
break;
} else {
arr[nx][ny] = '1';
q.add(new int[] { nx, ny });
}
}
}
}
System.out.printf("#%d %d%n", n, flag ? 1 : 0);
}
}
}