-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ_Maaaaaaaaaze.py
65 lines (49 loc) · 1.66 KB
/
BOJ_Maaaaaaaaaze.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
# 3차원 미로 최단거리 + 판 회전 (dfs)
from itertools import permutations
from collections import deque
board_init = [[input().split() for _ in range(5)] for _ in range(5)]
dist = [[[-1 for _ in range(5)] for _ in range(5)] for _ in range(5)]
dxyz = [[0,0,1], [0,0,-1], [1,0,0], [-1,0,0], [0,1,0], [0,-1,0]]
def valid(p):
return 0<=p[0]<5 and 0<=p[1]<5 and 0<=p[2]<5
def get_min_dist():
global ans
if board[0][0][0] == '0' or board[4][4][4] == '0':
return -1
for x in range(5):
for y in range(5):
for z in range(5):
dist[x][y][z] = -1
que = deque([(0,0,0)])
dist[0][0][0] = 0
while que:
p = que.popleft()
for d in dxyz:
a = p[0]+d[0], p[1]+d[1], p[2]+d[2]
if valid(a) and board[a[0]][a[1]][a[2]] == '1' and dist[a[0]][a[1]][a[2]] == -1:
dist[a[0]][a[1]][a[2]] = dist[p[0]][p[1]][p[2]] + 1
if dist[a[0]][a[1]][a[2]] < ans:
que.append(a)
return dist[4][4][4]
def rotated(arr):
return list(zip(*arr[::-1]))
ans = float('inf')
board = [[['-1' for _ in range(5)] for _ in range(5)] for _ in range(5)]
for p in permutations(range(5), 5):
for i in range(5):
board[i] = board_init[p[i]]
def dfs(i):
if i == 5:
mini = get_min_dist()
if mini != -1:
global ans
ans = min(ans, mini)
if mini == 12:
print(12)
exit(0)
return
for _ in range(4):
board[i] = rotated(board[i])
dfs(i+1)
dfs(0)
print(-1 if ans == float('inf') else ans)