-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.java
76 lines (61 loc) Β· 1.92 KB
/
Main.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
72
73
74
75
76
package BFS.P2178;
import java.io.*;
import java.util.*;
public class Main {
static int N, M;
static int[][] board;
static int[] di = {-1, 0, 1, 0};
static int[] dj = {0, 1, 0, -1};
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/BFS/P2178/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
board = new int[N][M];
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < M; j++) {
board[i][j] = line.charAt(j) - '0';
}
}
Queue<Position> q = new LinkedList<>();
q.offer(new Position(0, 0));
int move = 0;
loop: while (!q.isEmpty()) {
move ++;
int curSize = q.size();
for (int i = 0; i < curSize; i++) {
Position p = q.poll();
if (p.i == N-1 && p.j == M-1) break loop;
for (int j = 0; j < 4; j++) {
int to_i = p.i + di[j];
int to_j = p.j + dj[j];
if (isValidPath(to_i, to_j) && board[to_i][to_j] == 1) {
q.offer(new Position(to_i, to_j));
board[to_i][to_j] = 0;
}
}
}
}
System.out.println(move);
}
static boolean isValidPath(int i, int j){
return 0 <= i && i < N && 0 <= j && j < M;
}
}
class Position {
int i;
int j;
public Position(int i, int j) {
this.i = i;
this.j = j;
}
@Override
public String toString() {
return "Position{" +
"i=" + i +
", j=" + j +
'}';
}
}