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
77
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main2178_미로탐색 {
private static int[][] map;
private static boolean[][] visited;
private static int[] dx = {-1, 1 , 0 , 0};
private static int[] dy = {0, 0 , 1 , -1};
private static int N;
private static int M;
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());
M = Integer.parseInt(st.nextToken());
map= new int[N+1][M+1];
visited = new boolean[N+1][M+1];
for (int i = 1; i < N+1; i++) {
String str = br.readLine();
for (int j = 1; j < M+1; j++) {
map[i][j] = str.charAt(j-1)-'0';
}
}
bfs(1,1,1);
}//end of main
public static void bfs(int x, int y, int cnt) {
int count = 1;
Queue<Pair> q = new LinkedList<>();
visited[x][y] = true;
q.add(new Pair(x, y , cnt));
while(!q.isEmpty()) {
Pair p = q.poll();
count = p.cnt;
if(p.x == N && p.y ==M) {
System.out.println(count);
break;
}
for (int i = 0; i < dx.length; i++) {
int nx = p.x + dx[i];
int ny = p.y + dy[i];
if(nx > N || ny > M || visited[nx][ny] || map[nx][ny] ==0 ) continue;
else {
visited[nx][ny] = true;
q.add(new Pair(nx,ny, count+1));
}
}
}
}
static class Pair{
int x;
int y;
int cnt;
public Pair(int x, int y, int cnt) {
super();
this.x = x;
this.y = y;
this.cnt = cnt;
}
}
}//end of class
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
'Algorithm 문제풀이' 카테고리의 다른 글
[BOJ] 백준 1759 - 암호만들기 (JAVA) (0) | 2019.04.28 |
---|---|
[BOJ] 백준 6603 - 로또 (JAVA) (0) | 2019.04.27 |
백준1987 알파벳 (1) | 2019.04.11 |
백준2573 빙산 (4) | 2019.04.09 |
백준2468 안전영역 (2) | 2019.04.09 |