Skip to content

Commit c0e5547

Browse files
authored
Merge pull request #237 from AlgorithmWithGod/03do-new30
[20250312] BOJ / G3 / 캐슬 디펜스 / 신동윤
2 parents aa8330b + b10453f commit c0e5547

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
static int N, M, D, maxEnemy;
8+
static int[][] original;
9+
static int[] archerCols;
10+
11+
public static void main(String[] args) throws IOException {
12+
input();
13+
archerCols = new int[3];
14+
maxEnemy = 0;
15+
combination(0, 0);
16+
System.out.println(maxEnemy);
17+
}
18+
static void combination(int start, int idx) {
19+
if (idx == 3) {
20+
maxEnemy = Integer.max(maxEnemy, play());
21+
return;
22+
}
23+
for (int i = start; i < M; i++) {
24+
archerCols[idx] = i;
25+
combination(i + 1, idx + 1);
26+
}
27+
}
28+
29+
static int play() {
30+
// 게임을 플레이 할 임시 격자판 생성
31+
int[][] board = new int[N][M];
32+
for (int i = 0; i < N; i++) {
33+
for (int j = 0 ; j < M; j++) {
34+
board[i][j] = original[i][j];
35+
}
36+
}
37+
int total = 0;
38+
do {
39+
total += shoot(board);
40+
moveEnemy(board);
41+
} while(!isEmpty(board));
42+
return total;
43+
}
44+
45+
static void moveEnemy(int[][] board) {
46+
for (int r = N-2; r > -1; r--) {
47+
for (int c = 0; c < M; c++) {
48+
board[r+1][c] = board[r][c];
49+
}
50+
}
51+
// 맨 위 행은 0으로 채운다.
52+
Arrays.fill(board[0], 0);
53+
}
54+
static int shoot(int[][] board) {
55+
Set<Integer> killed = new HashSet<>();
56+
for (int archerCol : archerCols) {
57+
int minDist = D+1;
58+
int minLoc = -1;
59+
for (int r = 0; r < N; r++) {
60+
for (int c = 0; c < M; c++) {
61+
if (board[r][c] == 0) continue;
62+
int dist = getDist(N, archerCol, r, c);
63+
if (dist <= D) {
64+
if (dist < minDist || (dist == minDist && c < minLoc % M)) {
65+
minDist = dist;
66+
minLoc = r * M + c;
67+
}
68+
}
69+
}
70+
}
71+
if (minLoc == -1) { continue; }
72+
killed.add(minLoc);
73+
}
74+
75+
// 제거한 적 위치 0으로 전환
76+
for (int k : killed) {
77+
int kr = k / M;
78+
int kc = k % M;
79+
board[kr][kc] = 0;
80+
}
81+
return killed.size();
82+
}
83+
84+
static boolean isEmpty(int[][] board) {
85+
for (int i = 0; i < N; i++) {
86+
for (int j = 0; j < M; j++) {
87+
if (board[i][j] == 1) { return false; }
88+
}
89+
}
90+
return true;
91+
}
92+
static int getDist(int r1, int c1, int r2, int c2) {
93+
return Math.abs(r1-r2) + Math.abs(c1-c2);
94+
}
95+
static void input() throws IOException {
96+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
97+
StringTokenizer st = new StringTokenizer(br.readLine());
98+
N = Integer.parseInt(st.nextToken());
99+
M = Integer.parseInt(st.nextToken());
100+
D = Integer.parseInt(st.nextToken());
101+
original = new int[N][M];
102+
for (int i = 0; i < N; i++) {
103+
st = new StringTokenizer(br.readLine());
104+
for (int j = 0; j < M; j++) {
105+
original[i][j] = Integer.parseInt(st.nextToken());
106+
}
107+
}
108+
br.close();
109+
}
110+
}
111+
112+
```

0 commit comments

Comments
 (0)