This repository was archived by the owner on Dec 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ2178.java
More file actions
67 lines (54 loc) · 1.98 KB
/
BOJ2178.java
File metadata and controls
67 lines (54 loc) · 1.98 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class BOJ2178 {
static int n;
static int m;
static int[][] map;
static boolean[][] visited;
static int[] xDir = new int[]{-1, 0, 1, 0};
static int[] yDir = new int[]{0, 1, 0, -1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
map = new int[n + 2][m + 2];
visited = new boolean[n + 2][m + 2];
for (int i = 1; i <= n; i++) {
char[] input = br.readLine().toCharArray();
for (int j = 1; j <= m; j++) {
map[i][j] = input[j - 1] - '0';
}
}
Queue<Integer> xQueue = new LinkedList<>();
Queue<Integer> yQueue = new LinkedList<>();
Queue<Integer> lQueue = new LinkedList<>();
visited[1][1] = true;
xQueue.offer(1);
yQueue.offer(1);
lQueue.offer(1);
while (!xQueue.isEmpty()) {
int curX = xQueue.poll();
int curY = yQueue.poll();
int len = lQueue.poll();
if (curX == n && curY == m) {
System.out.println(len);
return;
}
for (int i = 0; i < 4; i++) {
int nextX = curX + xDir[i];
int nextY = curY + yDir[i];
if (map[nextX][nextY] == 1 && !visited[nextX][nextY]) {
xQueue.offer(nextX);
yQueue.offer(nextY);
lQueue.offer(len + 1);
visited[nextX][nextY] = true;
}
}
}
}
}