-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolution.java
95 lines (81 loc) Β· 2.8 KB
/
Solution.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
package Simulation.swea5656;
import java.io.*;
import java.util.*;
public class Solution {
static int T, N, W, H, ans;
static int[][] map, tmp;
static int[] di = {-1, 0, 1, 0};
static int[] dj = {0, 1, 0, -1};
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Simulation/swea5656/sample_input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
W = Integer.parseInt(st.nextToken());
H = Integer.parseInt(st.nextToken());
map = new int[H][W];
for (int i = 0; i < H; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < W; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
tmp = new int[H][W];
ans = W*H;
dfs(0, ans);
sb.append("#").append(t).append(" ").append(ans).append("\n");
}
System.out.println(sb.toString());
}
static void dfs(int cnt, int res) {
if (cnt == N || res == 0) {
ans = Math.min(ans, res);
return;
}
for (int w = 0; w < W; w++) {
for (int h = 0; h < H; h++) {
if (map[h][w] > 0) {
int[][] copy = new int[H][W];
for (int i = 0; i < H; i++) {
copy[i] = Arrays.copyOf(map[i], W);
tmp[i] = Arrays.copyOf(map[i], W);
}
crash(h, w);
dfs(cnt+1, down());
for (int i = 0; i < H; i++) map[i] = Arrays.copyOf(copy[i], W);
break;
}
}
}
}
static void crash(int i, int j) {
tmp[i][j] = 0;
for (int k = 0; k < 4; k++) {
for (int n = 1; n < map[i][j]; n++) {
int to_i = i + di[k]*n;
int to_j = j + dj[k]*n;
if (!isValidPath(to_i, to_j)) break;
if (tmp[to_i][to_j] > 0) crash(to_i, to_j);
}
}
}
static int down() {
int cnt = 0;
map = new int[H][W];
for (int j = 0; j < W; j++) {
for (int i = H-1, k = H-1; i >= 0; i--) {
if (tmp[i][j] > 0) {
map[k--][j] = tmp[i][j];
cnt ++;
}
}
}
return cnt;
}
static boolean isValidPath(int i, int j) {
return 0 <= i && i < H && 0 <= j && j < W;
}
}