-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek2_Day6.java
More file actions
73 lines (61 loc) · 2.12 KB
/
Week2_Day6.java
File metadata and controls
73 lines (61 loc) · 2.12 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
68
69
70
71
72
package Algorithm2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Queue;
public class Week2_Day6 {
public static HashSet<String> visit;
public static HashSet<String> okay;
public static int N;
public static int M;
public static int[] dx = {0, 0, 1, -1};
public static int[] dy = {1, -1, 0, 0};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] location = br.readLine().split(" ");
visit = new HashSet<>();
okay = new HashSet<>();
N = Integer.parseInt(location[0]);
M = Integer.parseInt(location[1]);
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < M; j++) {
String info = i + "-" + j;
int go = line.charAt(j) - '0';
if (go == 1) {
okay.add(info);
}
}
}
int result = bfs(0, 0);
System.out.println(result);
}
public static int bfs(int startX, int startY) {
Queue<int[]> qu = new ArrayDeque<>();
qu.offer(new int[]{startX, startY, 1});
visit.add(startX + "-" + startY);
while (!qu.isEmpty()) {
int[] current = qu.poll();
int x = current[0];
int y = current[1];
int num = current[2];
if (x == N - 1 && y == M - 1) {
return num;
}
for (int k = 0; k < 4; k++) {
int new_x = x + dx[k];
int new_y = y + dy[k];
String new_info = new_x + "-" + new_y;
if (0 <= new_x && new_x < N && 0 <= new_y && new_y < M) {
if (!visit.contains(new_info) && okay.contains(new_info)) {
qu.add(new int[]{new_x, new_y, num + 1});
visit.add(new_info);
}
}
}
}
return -1;
}
}