-
Notifications
You must be signed in to change notification settings - Fork 0
/
10472 십자뒤집기.py
38 lines (35 loc) · 1.12 KB
/
10472 십자뒤집기.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
# 십자뒤집기
# Gold 5, BFS
from collections import deque
def find(arr):
num = 0
for i in range(3):
for j in range(3):
if arr[i][j] == '*':
num |= 1<<(3*i+j)
return num
for _ in range(int(input())):
arr = [input() for _ in range(3)]
end = find(arr)
start = [['.']*3 for _ in range(3)]
q = deque([(start,0)])
visited = set([find(start)])
while q:
arr,count = q.popleft()
if find(arr) == end:
print(count)
break
for i in range(3):
for j in range(3):
narr = [['']*3 for _ in range(3)]
for ii in range(3):
for jj in range(3):
narr[ii][jj] = arr[ii][jj]
for ni,nj in (i,j),(i+1,j),(i-1,j),(i,j+1),(i,j-1):
if 0 <= ni < 3 and 0 <= nj < 3:
if arr[ni][nj] == '*': narr[ni][nj] = '.'
else: narr[ni][nj] = '*'
k = find(narr)
if k not in visited:
visited.add(k)
q.append((narr,count+1))