https://www.acmicpc.net/problem/16985
๋ธ๋ฃจํธํฌ์ค์ BFS๋ฅผ ์ด์ฉํ์ฌ ํ์๋ค
from itertools import permutations
from collections import deque
import sys
# ์
๋ ฅ ๋ฐ๋ ๋ถ๋ถ
maze = []
for i in range(5):
temp = []
for j in range(5):
temp.append(list(map(int, sys.stdin.readline().split())))
maze.append(temp)
dy = [0, 0, -1, 1, 0, 0]
dx = [-1, 1, 0, 0, 0, 0]
dz = [0, 0, 0, 0, -1, 1]
answer = 5 ** 4
# ์๊ณ๋ฐฉํฅ์ผ๋ก 90๋ ๋๋ฆฌ๊ธฐ
def rotate(arr):
result = [[0] * 5 for _ in range(5)]
for r in range(5):
for c in range(5):
result[c][4-r] = arr[r][c]
return result
# ์
๊ตฌ์์ ์ถ๊ตฌ๋ก ๋๋ฌํ๋ ์ต๋จ ๊ฒฝ๋ก ์ฐพ๊ธฐ
def bfs(temp):
result = 5 ** 4
if temp[0][0][0] == 1 and temp[4][4][4] == 1:
q = deque()
q.append([0, 0, 0])
visited = [[[-1] * 5 for _ in range(5)] for _ in range(5)]
visited[0][0][0] = 0
while q:
z, y, x = q.popleft()
if z == 4 and y == 4 and x == 4:
result = min(result, visited[z][y][x])
break
for i in range(6):
nz = z + dz[i]
ny = y + dy[i]
nx = x + dx[i]
if 0 <= nz < 5 and 0 <= ny < 5 and 0 <= nx < 5:
if visited[nz][ny][nx] == -1 and temp[nz][ny][nx] == 1:
visited[nz][ny][nx] = visited[z][y][x] + 1
q.append([nz, ny, nx])
return result
# ๋ฏธ๋ก ๋ง๋๋ ๋ชจ๋ ๊ฒฝ์ฐ ๊ตฌํ๊ธฐ
for p in list(permutations([0, 1, 2, 3, 4], 5)):
temp = [maze[p[0]], maze[p[1]], maze[p[2]], maze[p[3]], maze[p[4]]]
for a in range(4):
temp[0] = rotate(temp[0])
for b in range(4):
temp[1] = rotate(temp[1])
for c in range(4):
temp[2] = rotate(temp[2])
for d in range(4):
temp[3] = rotate(temp[3])
for e in range(4):
temp[4] = rotate(temp[4])
answer = min(answer, bfs(temp))
if answer == 5 ** 4:
print(-1)
else:
print(answer)
๋ฏธ๋กํ ์๊ธฐ๋ ์์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํ์๊ณ ํ์ ์ ๋ฌด์ํ๊ฒ 5์ค for๋ฌธ์ ์ด์ฉํ์๋คใ
5๊ฐ์ ๋ฏธ๋กํ์ ๊ฐ๊ฐ 0๋, 90๋, 180๋, 270๋ ํ์ ํ ์ ์์ผ๋ฏ๋ก ๊ฒฝ์ฐ์ ์๋ 4x4x4x4x4 โ 5์ค for๋ฌธ ์ฌ์ฉํด์ผํ๋ค
โ
๋ ํท๊ฐ๋ ธ๋ ๋ถ๋ถ์ด
"ํ๋ธ์ ์
๊ตฌ๋ ์ ์ก๋ฉด์ฒด์์ ์ฐธ๊ฐ์๊ฐ ์์๋ก ์ ํํ ๊ผญ์ง์ ์ ์์นํ ์นธ"์ด๋ผ๋ ๋ด์ฉ์ด์๋ค
์ฒจ์ ์ด ๋ด์ฉ ๋๋ฌธ์ ์๋ ์ฝ๋์ ๊ฐ์ด 8๊ฐ์ ๊ผญ์ง์ ๊ฐ๊ฐ์ ์์์ ์ผ๋ก BFS๋ฅผ ๋๋ฆฌ๋ ์ฝ๋๋ก ์งฐ๋๋ ์๊ฐ์ด๊ณผ๊ฐ ๋ด์๋ค
### โ์๊ฐ์ด๊ณผ ์ฝ๋โ ###
from itertools import permutations
from collections import deque
import sys
maze = []
for i in range(5):
temp = []
for j in range(5):
temp.append(list(map(int, sys.stdin.readline().split())))
maze.append(temp)
start = [[0, 0, 0], [0, 0, 4], [0, 4, 0], [0, 4, 4],
[4, 0, 0], [4, 0, 4], [4, 4, 0], [4, 4, 4]]
end = [[4, 4, 4], [4, 4, 0], [4, 0, 4], [4, 0, 0],
[0, 4, 4], [0, 4, 0], [0, 0, 4], [0, 0, 0]]
dy = [0, 0, -1, 1, 0, 0]
dx = [-1, 1, 0, 0, 0, 0]
dz = [0, 0, 0, 0, -1, 1]
answer = 5 ** 4
def rotate(arr):
result = [[0] * 5 for _ in range(5)]
for r in range(5):
for c in range(5):
result[c][4-r] = arr[r][c]
return result
def bfs(temp):
result = 5 ** 4
for k in range(8):
sz, sy, sx = start[k][0], start[k][1], start[k][2] # ์์์
ez, ey, ex = end[k][0], end[k][1], end[k][2] # ๋์
if temp[sz][sy][sx] == 1 and temp[ez][ey][ex] == 1:
q = deque()
q.append([sz, sy, sx])
visited = [[[-1] * 5 for _ in range(5)] for _ in range(5)]
visited[sz][sy][sx] = 0
while q:
z, y, x = q.popleft()
if z == ez and y == ey and x == ex:
result = min(result, visited[z][y][x])
break
for i in range(6):
nz = z + dz[i]
ny = y + dy[i]
nx = x + dx[i]
if 0 <= nz < 5 and 0 <= ny < 5 and 0 <= nx < 5:
if visited[nz][ny][nx] == -1 and temp[nz][ny][nx] == 1:
visited[nz][ny][nx] = visited[z][y][x] + 1
q.append([nz, ny, nx])
return result
for p in list(permutations([0, 1, 2, 3, 4], 5)):
temp = [maze[p[0]], maze[p[1]], maze[p[2]], maze[p[3]], maze[p[4]]]
for a in range(4):
temp[0] = rotate(temp[0])
for b in range(4):
temp[1] = rotate(temp[1])
for c in range(4):
temp[2] = rotate(temp[2])
for d in range(4):
temp[3] = rotate(temp[3])
for e in range(4):
temp[4] = rotate(temp[4])
answer = min(answer, bfs(temp))
if answer == 5 ** 4:
print(-1)
else:
print(answer)
ํ์ง๋ง ํ์ +์๊ธฐ์ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ์ด๋ฏธ ๊ณ ๋ คํด์ฃผ๊ธฐ ๋๋ฌธ์ ๊ตณ์ด 8๊ฐ์ ์์์ ์ ๋ํด ๋ ๊ณ ๋ คํด์ค ํ์๊ฐ ์์๋ค
๊ทธ๋๊น ํ์ +์๊ธฐ์ ๊ฒฝ์ฐ๋ค ์์ ํ๊ฐ์ง ํ๋ธ ๋ชจ์์ ๋ํด 8๊ฐ์ ์
๊ตฌ๋ก ์์ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ชจ๋ ํฌํจ๋๋ค
์ ์ถ๊ฒฐ๊ณผ pypy๋ก๋ง ํต๊ณผ
๋์ค์ ์๊ฐ ๋ ์ค์ด๋ ๋ฐฉ๋ฒ์ ์๊ฒ๋๋ฉด ์
๋ฐ์ดํธ ํด์ผ๊ฒ ๋น