[๋ฐฑ์ค€ ๐Ÿฅ‡2] 16985๋ฒˆ Maaaaaaaaaze (Python/ํŒŒ์ด์ฌ)

mingssoยท2023๋…„ 3์›” 19์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
21/35

1๏ธโƒฃ ๋ฌธ์ œ

https://www.acmicpc.net/problem/16985



2๏ธโƒฃ ์ฝ”๋“œ

๋ธŒ๋ฃจํŠธํฌ์Šค์™€ 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)



3๏ธโƒฃ ํ’€์ด

๋ฏธ๋กœํŒ ์Œ“๊ธฐ๋Š” ์ˆœ์—ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์˜€๊ณ  ํšŒ์ „์€ ๋ฌด์‹ํ•˜๊ฒŒ 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๋กœ๋งŒ ํ†ต๊ณผ
๋‚˜์ค‘์— ์‹œ๊ฐ„ ๋” ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ฒŒ๋˜๋ฉด ์—…๋ฐ์ดํŠธ ํ•ด์•ผ๊ฒ ๋‹น

profile
๐Ÿฅ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ’ฐ

0๊ฐœ์˜ ๋Œ“๊ธ€