-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1261.java
71 lines (60 loc) · 2 KB
/
1261.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class AlgoSpot {
static int N, M;
static int[][] map;
static int[][] dir = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0}
};
private static class Point {
int y;
int x;
int count;
Point(int y, int x, int count) {
this.y = y;
this.x = x;
this.count = count;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
M = Integer.parseInt(input[0]);
N = Integer.parseInt(input[1]);
map = new int[N][M];
for(int i = 0; i < N; i++) {
input = br.readLine().split("");
for(int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(input[j]);
}
}
br.close();
BFS();
}
public static void BFS() {
PriorityQueue<Point> priorityQueue = new PriorityQueue<>(((o1, o2) -> o1.count - o2.count));
priorityQueue.add(new Point(0, 0, 0));
boolean[][] visited = new boolean[N][M];
while(!priorityQueue.isEmpty()) {
Point point = priorityQueue.poll();
if(point.count > 200) continue;
if(point.y == N - 1 && point.x == M - 1) {
System.out.println(point.count);
return;
}
for(int i = 0; i < 4; i++) {
int ny = point.y + dir[i][0];
int nx = point.x + dir[i][1];
if(ny < 0 || ny >= N || nx < 0 || nx >= M || visited[ny][nx]) continue;
if(map[ny][nx] == 1) priorityQueue.add(new Point(ny, nx, point.count + 1));
else priorityQueue.add(new Point(ny, nx, point.count));
visited[ny][nx] = true;
}
}
}
}