-
Notifications
You must be signed in to change notification settings - Fork 0
/
14497.java
111 lines (83 loc) · 2.95 KB
/
14497.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.io.*;
import java.util.*;
public class Main {
static int N;
static int M;
static int[] row = {1, -1, 0, 0};
static int[] col = {0, 0, 1, -1};
static int startX;
static int startY;
static int endX;
static int endY;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
startX = Integer.parseInt(st.nextToken());
startY = Integer.parseInt(st.nextToken());
endX = Integer.parseInt(st.nextToken());
endY = Integer.parseInt(st.nextToken());
char[][] board = new char[N][M];
boolean[][] visited = new boolean[N][M];
startX--;
startY--;
endX--;
endY--;
for(int i=0;i<N;i++){
st = new StringTokenizer(br.readLine());
String str = st.nextToken();
for(int j=0;j<M;j++){
board[i][j] = str.charAt(j);
}
}
int ans = 1;
while (!change(board, new boolean[N][M])) {
/*System.out.println("ans = " + ans);
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
System.out.print(board[i][j] + " ");
}
System.out.println();
}*/
++ans;
/*if(ans == 10){
break;
}*/
}
bw.write(ans+"\n");
bw.flush();
}
public static boolean change(char[][] board, boolean[][] visited){
// 상 하 좌 우 (방문하지 않았고, 벽이나 1을 만나기 전까지)
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{startX, startY});
visited[startX][startY] = true;
List<int[]> deleteList = new ArrayList<>();
while (!queue.isEmpty()) {
int[] cur = queue.poll();
if (cur[0] == endX && cur[1] == endY) {
return true;
}
for(int i=0;i<4;i++){
int nx = cur[0] + row[i];
int ny = cur[1] + col[i];
if (0 <= nx && nx < N && 0 <= ny && ny < M) {
if(!visited[nx][ny] && (board[nx][ny] == '0' || board[nx][ny] == '#')){
visited[nx][ny] = true;
queue.add(new int[]{nx, ny});
} else if(board[nx][ny] == '1'){
visited[nx][ny] = true;
deleteList.add(new int[]{nx, ny});
}
}
}
}
for (int[] point : deleteList) {
board[point[0]][point[1]] = '0';
}
return false;
}
}