Skip to content

Commit ddc6027

Browse files
committed
[Simulation] swea-5656
1 parent 366c989 commit ddc6027

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
| 25 | | [Baekjoon-17779 게리맨더링 2](./src/Simulation/P17779) | |
6565
| 26 | | [Baekjoon-2564 경비원](./src/Simulation/P2564) | |
6666
| 27 | | [Baekjoon-17144 미세먼지 안녕!](./src/Simulation/P17144) | |
67+
| 28 | | [SWEA-5656 벽돌 깨기](./src/Simulation/swea5656) | |
6768

6869
## String
6970

Diff for: src/Simulation/swea5656/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## [SW Expert Academy - 5656] 벽돌 깨기
2+
3+
![1](https://user-images.githubusercontent.com/22045163/114741910-31b42480-9d86-11eb-8383-03266eb936a8.jpg)
4+
![2](https://user-images.githubusercontent.com/22045163/114741905-311b8e00-9d86-11eb-8638-16fbb2319616.jpg)
5+
![3](https://user-images.githubusercontent.com/22045163/114741900-2fea6100-9d86-11eb-957b-d110d95d36ea.jpg)
6+
![4](https://user-images.githubusercontent.com/22045163/114741876-2a8d1680-9d86-11eb-9aaf-35800c9a7c6a.jpg)
7+
8+
![image](https://user-images.githubusercontent.com/22045163/114741252-899e5b80-9d85-11eb-9e43-38674cd79077.png)
9+
10+
오우 진짜 어렵다 ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ

Diff for: src/Simulation/swea5656/Solution.java

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package Simulation.swea5656;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Solution {
7+
8+
static int T, N, W, H, ans;
9+
static int[][] map, tmp;
10+
static int[] di = {-1, 0, 1, 0};
11+
static int[] dj = {0, 1, 0, -1};
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/Simulation/swea5656/sample_input.txt"));
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringBuilder sb = new StringBuilder();
17+
18+
T = Integer.parseInt(br.readLine());
19+
for (int t = 1; t <= T; t++) {
20+
StringTokenizer st = new StringTokenizer(br.readLine());
21+
N = Integer.parseInt(st.nextToken());
22+
W = Integer.parseInt(st.nextToken());
23+
H = Integer.parseInt(st.nextToken());
24+
25+
map = new int[H][W];
26+
for (int i = 0; i < H; i++) {
27+
st = new StringTokenizer(br.readLine());
28+
for (int j = 0; j < W; j++) {
29+
map[i][j] = Integer.parseInt(st.nextToken());
30+
}
31+
}
32+
33+
tmp = new int[H][W];
34+
ans = W*H;
35+
dfs(0, ans);
36+
37+
sb.append("#").append(t).append(" ").append(ans).append("\n");
38+
}
39+
40+
System.out.println(sb.toString());
41+
}
42+
43+
static void dfs(int cnt, int res) {
44+
if (cnt == N || res == 0) {
45+
ans = Math.min(ans, res);
46+
return;
47+
}
48+
49+
for (int w = 0; w < W; w++) {
50+
for (int h = 0; h < H; h++) {
51+
if (map[h][w] > 0) {
52+
int[][] copy = new int[H][W];
53+
for (int i = 0; i < H; i++) {
54+
copy[i] = Arrays.copyOf(map[i], W);
55+
tmp[i] = Arrays.copyOf(map[i], W);
56+
}
57+
crash(h, w);
58+
dfs(cnt+1, down());
59+
for (int i = 0; i < H; i++) map[i] = Arrays.copyOf(copy[i], W);
60+
break;
61+
}
62+
}
63+
}
64+
}
65+
66+
static void crash(int i, int j) {
67+
tmp[i][j] = 0;
68+
for (int k = 0; k < 4; k++) {
69+
for (int n = 1; n < map[i][j]; n++) {
70+
int to_i = i + di[k]*n;
71+
int to_j = j + dj[k]*n;
72+
if (!isValidPath(to_i, to_j)) break;
73+
if (tmp[to_i][to_j] > 0) crash(to_i, to_j);
74+
}
75+
}
76+
}
77+
78+
static int down() {
79+
int cnt = 0;
80+
map = new int[H][W];
81+
for (int j = 0; j < W; j++) {
82+
for (int i = H-1, k = H-1; i >= 0; i--) {
83+
if (tmp[i][j] > 0) {
84+
map[k--][j] = tmp[i][j];
85+
cnt ++;
86+
}
87+
}
88+
}
89+
return cnt;
90+
}
91+
92+
static boolean isValidPath(int i, int j) {
93+
return 0 <= i && i < H && 0 <= j && j < W;
94+
}
95+
}

Diff for: src/Simulation/swea5656/sample_input.txt

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
5
2+
3 10 10
3+
0 0 0 0 0 0 0 0 0 0
4+
1 0 1 0 1 0 0 0 0 0
5+
1 0 3 0 1 1 0 0 0 1
6+
1 1 1 0 1 2 0 0 0 9
7+
1 1 4 0 1 1 0 0 1 1
8+
1 1 4 1 1 1 2 1 1 1
9+
1 1 5 1 1 1 1 2 1 1
10+
1 1 6 1 1 1 1 1 2 1
11+
1 1 1 1 1 1 1 1 1 5
12+
1 1 7 1 1 1 1 1 1 1
13+
2 9 10
14+
0 0 0 0 0 0 0 0 0
15+
0 0 0 0 0 0 0 0 0
16+
0 1 0 0 0 0 0 0 0
17+
0 1 0 0 0 0 0 0 0
18+
1 1 0 0 1 0 0 0 0
19+
1 1 0 1 1 1 0 1 0
20+
1 1 0 1 1 1 0 1 0
21+
1 1 1 1 1 1 1 1 0
22+
1 1 3 1 6 1 1 1 1
23+
1 1 1 1 1 1 1 1 1
24+
3 6 7
25+
1 1 0 0 0 0
26+
1 1 0 0 1 0
27+
1 1 0 0 4 0
28+
4 1 0 0 1 0
29+
1 5 1 0 1 6
30+
1 2 8 1 1 6
31+
1 1 1 9 2 1
32+
4 4 15
33+
0 0 0 0
34+
0 0 0 0
35+
0 0 0 0
36+
1 0 0 0
37+
1 0 0 0
38+
1 0 0 0
39+
1 0 0 0
40+
1 0 5 0
41+
1 1 1 0
42+
1 1 1 9
43+
1 1 1 1
44+
1 6 1 2
45+
1 1 1 5
46+
1 1 1 1
47+
2 1 1 2
48+
4 12 15
49+
9 9 9 9 9 9 9 9 9 9 9 9
50+
9 9 9 9 9 9 9 9 9 9 9 9
51+
9 9 9 9 9 9 9 9 9 9 9 9
52+
9 9 9 9 9 9 9 9 9 9 9 9
53+
9 9 9 9 9 9 9 9 9 9 9 9
54+
9 9 9 9 9 9 9 9 9 9 9 9
55+
9 9 9 9 9 9 9 9 9 9 9 9
56+
9 9 9 9 9 9 9 9 9 9 9 9
57+
9 9 9 9 9 9 9 9 9 9 9 9
58+
9 9 9 9 9 9 9 9 9 9 9 9
59+
9 9 9 9 9 9 9 9 9 9 9 9
60+
9 9 9 9 9 9 9 9 9 9 9 9
61+
9 9 9 9 9 9 9 9 9 9 9 9
62+
9 9 9 9 9 9 9 9 9 9 9 9
63+
9 9 9 9 9 9 9 9 9 9 9 9

Diff for: src/Simulation/swea5656/sample_output.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#1 12
2+
#2 27
3+
#3 4
4+
#4 8
5+
#5 0

0 commit comments

Comments
 (0)