Skip to content

Commit c9beec7

Browse files
authored
[20251006] BOJ / P5 / 비숍 / 한종욱
1 parent 3a922c4 commit c9beec7

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

Ukj0ng/202510/06 BOJ P5 비숍.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static boolean[][] board, visited;
9+
private static int N, answer;
10+
11+
public static void main(String[] args) throws IOException {
12+
init();
13+
14+
DFS(0, 0, true);
15+
int blackMax = answer;
16+
answer = 0;
17+
18+
visited = new boolean[N][N];
19+
DFS(0, 0, false);
20+
int whiteMax = answer;
21+
answer = blackMax + whiteMax;
22+
23+
bw.write(answer + "\n");
24+
bw.flush();
25+
bw.close();
26+
br.close();
27+
}
28+
29+
private static void init() throws IOException {
30+
N = Integer.parseInt(br.readLine());
31+
answer = 0;
32+
33+
board = new boolean[N][N];
34+
visited = new boolean[N][N];
35+
36+
for (int i = 0; i < N; i++) {
37+
StringTokenizer st = new StringTokenizer(br.readLine());
38+
for (int j = 0; j < N; j++) {
39+
if ("1".equals(st.nextToken())) board[i][j] = true;
40+
}
41+
}
42+
}
43+
44+
private static void DFS(int index, int count, boolean isBlack) {
45+
answer = Math.max(answer, count);
46+
47+
for (int i = index; i < N * N; i++) {
48+
int x = i / N;
49+
int y = i % N;
50+
51+
if ((x + y) % 2 == (isBlack ? 0 : 1)) continue;
52+
53+
if (board[x][y] && canPlace(x, y)) {
54+
visited[x][y] = true;
55+
DFS(i + 1, count + 1, isBlack);
56+
visited[x][y] = false;
57+
}
58+
}
59+
}
60+
61+
private static boolean canPlace(int x, int y) {
62+
for (int i = 1; x+i < N && y+i < N; i++) {
63+
if (visited[x+i][y+i]) return false;
64+
}
65+
for (int i = 1; x+i < N && y-i >= 0; i++) {
66+
if (visited[x+i][y-i]) return false;
67+
}
68+
for (int i = 1; x-i >= 0 && y+i < N; i++) {
69+
if (visited[x-i][y+i]) return false;
70+
}
71+
for (int i = 1; x-i >= 0 && y-i >= 0; i++) {
72+
if (visited[x-i][y-i]) return false;
73+
}
74+
return true;
75+
}
76+
}
77+
```

0 commit comments

Comments
 (0)