백준 침투
풀이
bfs를 통해 해결할 수 있는 문제
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
78
79
80
81
82
83
84
85
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class Pos{
int x;
int y;
public Pos(int y, int x){
this.y = y;
this.x = x;
}
}
public class Main {
public static ArrayList<ArrayList<Integer>> list = new ArrayList<>();
public static boolean visited[][];
public static int dx[] = {0, 0, -1, 1};
public static int dy[] = {-1, 1, 0, 0};
public static boolean ans = false;
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
visited = new boolean[N][M];
for(int i=0; i<N; i++) list.add(new ArrayList<>());
for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
String str = st.nextToken();
for(int j=0; j<M; j++){
list.get(i).add(str.charAt(j) - 48);
}
}
for(int i=0; i<M; i++){
if(list.get(0).get(i) == 0)
bfs(new Pos(0, i), N, M);
}
if(ans)
System.out.println("YES");
else
System.out.println("NO");
}
public static void bfs(Pos start, int N, int M){
Queue<Pos> queue = new LinkedList<>();
queue.add(start);
visited[0][start.x] = true;
while(!queue.isEmpty()){
Pos cur = queue.poll();
if(cur.y == N-1){
ans = true; return;
}
for(int i=0; i<4; i++) {
int nextY = cur.y + dy[i];
int nextX = cur.x + dx[i];
if (nextX >= 0 && nextX < M && nextY >= 0 && nextY < N) {
if (!visited[nextY][nextX] && list.get(nextY).get(nextX) == 0) {
visited[nextY][nextX] = true;
queue.add(new Pos(nextY, nextX));
}
}
}
}
}
}