-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ2665.java
99 lines (77 loc) · 2.77 KB
/
BOJ2665.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static final int BLACK = 0;
public static final int WHITE = 1;
public static int N;
public static int[][] board;
public static int[] dy = {-1, 1, 0, 0};
public static int[] dx = {0, 0, -1, 1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
board = new int[N][N];
for (int i = 0; i < N; ++i) {
String rowStr = br.readLine();
for (int j = 0; j < rowStr.length(); ++j) {
char c = rowStr.charAt(j);
int val = c - '0';
board[i][j] = val;
}
}
int ans = Integer.MAX_VALUE;
int minv = 0;
int maxv = N*N + 1;
while (minv <= maxv) {
int mid = (minv + maxv) / 2;
if (isPossible(mid)) {
ans = Math.min(ans, mid);
maxv = mid - 1;
} else {
minv = mid + 1;
}
}
System.out.println(ans);
}
public static boolean isPossible(int changeCnt) {
Queue<Integer[]> queue = new LinkedList<>();
boolean[][][] visited;
visited = new boolean[changeCnt + 1][N][N];
visited[changeCnt][0][0] = true;
queue.add(new Integer[]{changeCnt,0,0});
while (!queue.isEmpty()) {
Integer[] now = queue.poll();
int breakCnt = now[0];
int nowY = now[1];
int nowX = now[2];
if (nowY == N - 1 && nowX == N - 1) {
return true;
}
for (int dir = 0; dir < 4; ++dir) {
int ny = nowY + dy[dir];
int nx = nowX + dx[dir];
if (isSafe(ny, nx)) {
if (breakCnt > 0 && board[ny][nx] == BLACK) {
int nxtBreakCnt = breakCnt - 1;
if (!visited[nxtBreakCnt][ny][nx]) {
visited[nxtBreakCnt][ny][nx] = true;
queue.add(new Integer[]{nxtBreakCnt, ny, nx});
}
} else if (board[ny][nx] == WHITE) {
if (!visited[breakCnt][ny][nx]) {
visited[breakCnt][ny][nx] = true;
queue.add(new Integer[]{breakCnt, ny, nx});
}
}
}
}
}
return false;
}
public static boolean isSafe(int y, int x) {
return 0 <= y && y < N && 0 <= x && x < N;
}
}