-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path201007_5656_벽돌깨기_모의SW역량테스트.py
64 lines (54 loc) · 1.67 KB
/
201007_5656_벽돌깨기_모의SW역량테스트.py
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
from itertools import product
import copy
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
def breaks(y, x):
global tmp
if tmp[y][x] > 0:
t = tmp[y][x]
tmp[y][x] = 0
for it in range(1, t):
for i in range(4):
nx = x + dx[i] * it
ny = y + dy[i] * it
if 0 <= nx < W and 0 <= ny < H and tmp[ny][nx] > 0:
breaks(ny, nx)
T = int(input())
for t in range(1, T + 1):
inp = list(map(int, input().split()))
N = inp[0];
W = inp[1];
H = inp[2];
board = []
C = list(product([i for i in range(W)], repeat=N))
for h in range(H):
line = list(map(int, input().split()))
board.append(line)
cnt = H * W
for c in C:
tmp = copy.deepcopy(board)
for x in c:
for y in range(H):
if tmp[y][x] > 0:
breaks(y, x)
for idx in range(W):
s = []
for idy in range(H):
if tmp[idy][idx] > 0:
s.append(tmp[idy][idx])
for p in range(H - len(s)):
tmp[p][idx] = 0
for p in range(H - len(s), H):
tmp[p][idx] = s[p - H + len(s)]
break
new_cnt = 0
for i in range(H - 1, -1, -1): # 카운트
if tmp[i].count(0) >= W:
break
for j in range(W):
if tmp[i][j] > 0:
new_cnt += 1
cnt = min(cnt, new_cnt)
if cnt == 0:
break
print('#' + str(t) + ' ' + str(cnt))