-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2178.java
More file actions
54 lines (45 loc) · 1.55 KB
/
2178.java
File metadata and controls
54 lines (45 loc) · 1.55 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
/**
* 백준 2178번 미로 탐색
* bfs 알고리즘
*/
import java.util.*;
import java.awt.*;
import java.io.*;
public class Main {
static int n;
static int m;
static int map[][] = new int[100][100];
static int visited[][] = new int[100][100];
static int direction[][] = {{0,1},{0,-1},{1,0},{-1,0}}; // 우, 좌, 하, 상
static void bfs(){
Deque<Point> queue = new ArrayDeque<>();
queue.add(new Point(0,0));
visited[0][0] = 1;
while(!queue.isEmpty()){
Point now = queue.poll();
for(int[] d : direction){
int newY = now.y+d[0];
int newX = now.x+d[1];
if(0<=newY&&newY<n && 0<=newX&&newX<m && visited[newY][newX]==0 && map[newY][newX]!=0){
queue.add(new Point(newX,newY));
visited[newY][newX] = 1;
map[newY][newX] = map[now.y][now.x] + 1;
}
}
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
n = Integer.parseInt(st.nextToken()); // y
m = Integer.parseInt(st.nextToken()); // x
for(int i=0; i<n; i++){
String s = br.readLine();
for(int j=0; j<m; j++){
map[i][j] = s.charAt(j) - '0';
}
}
bfs();
System.out.println(map[n-1][m-1]);
}
}