-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ2178.java
More file actions
55 lines (45 loc) · 1.11 KB
/
Copy pathBOJ2178.java
File metadata and controls
55 lines (45 loc) · 1.11 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
package day7.graph;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class BOJ2178 {
static int N, M;
static int[][] arr;
static boolean[][] visited;
static int[] dx= {0,1,-1,0};
static int[] dy= {-1,0,0,1};
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
N=sc.nextInt();
M=sc.nextInt();
arr=new int[N][M];
visited=new boolean[N][M];
for (int i = 0; i < N; i++) {
String[] str=sc.next().split("");
for (int j = 0; j < M; j++) {
arr[i][j]=Integer.parseInt(str[j]);
}
}
BFS(0,0);
System.out.println(arr[N-1][M-1]);
}
private static void BFS(int s1, int s2) {
Queue<int[]> queue=new LinkedList<>();
queue.offer(new int[] {s1,s2});
visited[s1][s2]=true;
while(!queue.isEmpty()) {
int curr[]=queue.poll();
for (int i = 0; i <4; i++) {
int x=curr[0]+dx[i];
int y=curr[1]+dy[i];
if(x >= 0 && x < N && y >= 0 && y < M) {
if(arr[x][y] !=0 && !visited[x][y]) {
arr[x][y]=arr[curr[0]][curr[1]]+1;
queue.offer(new int[] {x,y});
visited[x][y]=true;
}
}
}
}
}
}