-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_7_2.py
More file actions
64 lines (55 loc) · 1.74 KB
/
Copy pathproblem_7_2.py
File metadata and controls
64 lines (55 loc) · 1.74 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
def rotate(block):
return list(zip(*reversed(block)))
def generate_rotations(block):
rotations = [ [] for _ in range(4)]
for rot in range(4):
originY = originX = -1
for i in range(len(block)):
for j in range(len(block[0])):
if block[i][j] == 1:
if originY == -1:
originY, originX = i, j
rotations[rot].append((i - originY, j - originX))
block = rotate(block)
blocks = {tuple(block) for block in rotations} # 중복 제거
return list(blocks)
def search(placed):
global H, W, board, rotations, best
y, x = find_white(board, H, W)
if y == -1:
best = max(best, placed)
else:
for i in range(len(rotations)):
if place(y, x, rotations[i], 1):
search(placed + 1)
place(y, x, rotations[i], -1)
board[y][x] = 1
search(placed)
board[y][x] = 0
def place(y, x, block, delta):
global H, W, board
ok = True
for i in range(len(block)):
ny = y + block[i][0]
nx = x + block[i][1]
if not ((0 <= ny < H) and (0 <= nx < W)):
ok = False
else:
board[ny][nx] += delta
if (board[ny][nx] > 1):
ok = False
return ok
def find_white(board, H, W):
for i in range(H):
for j in range(W):
if board[i][j] == 0:
return i, j
return -1, -1
for _ in range(int(input())):
H, W, R, C = map(int, input().split())
board = [[[0,1][x =='#'] for x in input()] for _ in range(H)]
block = [[[0,1][x == '#'] for x in input()] for _ in range(R)]
rotations = generate_rotations(block)
best = 0
search(0)
print(best)