This repository was archived by the owner on Dec 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSW5656.java
More file actions
146 lines (121 loc) · 4.39 KB
/
SW5656.java
File metadata and controls
146 lines (121 loc) · 4.39 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.util.Scanner;
public class SW5656 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int a = 1; a <= t; a++) {
int n = sc.nextInt();
int w = sc.nextInt();
int h = sc.nextInt();
int[][] blockInput = new int[h + 1][w + 1];
int[][] map = new int[h + 1][w + 1];
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
blockInput[i][j] = sc.nextInt();
}
}
int[] B = new int[4];
int answer = Integer.MAX_VALUE;
for (B[0] = 1; B[0] <= w; B[0]++) {
for (B[1] = 1; B[1] <= w; B[1]++) {
for (B[2] = 1; B[2] <= w; B[2]++) {
for (B[3] = 1; B[3] <= w; B[3]++) {
init(w, h, blockInput, map);
for (int i = 0; i < n; i++){
bomb(w, h, map, B[i]);
down(w, h, map);
}
answer = block_check(w, h, map, answer);
if (n < 4) break;
}
if (n < 3) break;
}
if (n < 2) break;
}
}
System.out.println("#" + a + " " + answer);
}
}
private static void bomb(int w, int h, int[][] map, int i) {
for (int j = 1; j <= h; j++) {
if (map[j][i] == 1) {
map[j][i] = 0;
return;
}
else if (map[j][i] > 1) {
map[j][i] += 10;
break;
}
}
boolean flag = true;
while (flag) {
flag = boom(w, h, map);
}
}
private static int block_check(int w, int h, int[][] map, int answer) {
int count = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j<= w; j++) {
if (map[i][j] > 0) count++;
}
}
return Math.min(count, answer);
}
private static void down(int w, int h, int[][] map) {
for (int i = 1; i <= w; i++) {
for (int j = h; j > 0; j--) {
if (map[j][i] == 0) {
for (int k = j - 1; k > 0; k--) {
if (map[k][i] > 0) {
map[j][i] = map[k][i];
map[k][i] = 0;
break;
}
}
}
}
}
}
private static boolean boom(int w, int h, int[][] map) {
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (map[i][j] > 10) {
int d = map[i][j] - 10;
map[i][j] = 0;
boolean flag = false;
int left = Math.max(j - d + 1, 1);
int right = Math.min(j + d, w + 1);
for (int k = left; k < right; k++) {
if (map[i][k] == 1) {
map[i][k] = 0;
}
else if (map[i][k] > 1 && map[i][k] < 10) {
map[i][k] += 10;
flag = true;
}
}
int up = Math.max(i - d + 1, 1);
int down = Math.min(i + d, h + 1);
for (int k = up; k < down; k++) {
if (map[k][j] == 1) {
map[k][j] = 0;
}
else if (map[k][j] > 1 && map[k][j] < 10) {
map[k][j] += 10;
flag = true;
}
}
if (flag) return true;
}
}
}
return false;
}
private static void init(int w, int h, int[][] blockInput, int[][] map) {
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
map[i][j] = blockInput[i][j];
}
}
}
}