-
Notifications
You must be signed in to change notification settings - Fork 6
/
dongwoo.java
56 lines (52 loc) · 1.58 KB
/
dongwoo.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class dongwoo {
static int[] dr = { 0, 1, 0, -1 };
static int[] dc = { 1, 0, -1, 0 };
public static void main(String[] args) throws IOException {
// bfs로 거리계산
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int M = Integer.parseInt(st.nextToken());
int N = Integer.parseInt(st.nextToken());
int[][] map = new int[N][M];
int[][] distance = new int[N][M];
PriorityQueue<int[]> q = new PriorityQueue<int[]>((o1, o2) -> o1[2] - o2[2]);
for (int i = 0; i < N; i++) {
String str = br.readLine();
for (int j = 0; j < M; j++) {
map[i][j] = str.charAt(j) - '0';
distance[i][j] = Integer.MAX_VALUE;
}
}
q.add(new int[] { 0, 0, 0 });
distance[0][0] = 0;
while (!q.isEmpty()) {
int[] now = q.poll();
if (now[0] == N - 1 && now[1] == M - 1) {
System.out.println(now[2]);
break;
}
for (int i = 0; i < 4; i++) {
int newR = now[0] + dr[i];
int newC = now[1] + dc[i];
int val;
try {
val = map[newR][newC];
} catch (IndexOutOfBoundsException e) {
continue;
}
if (val != 0 && distance[newR][newC] > now[2] + 1) {
distance[newR][newC] = now[2] + 1;
q.add(new int[] { newR, newC, now[2] + 1 });
} else if (val == 0 && distance[newR][newC] > now[2]) {
distance[newR][newC] = now[2];
q.add(new int[] { newR, newC, now[2] });
}
}
}
}
}