본문 바로가기

[프로그래머스] 아이템 줍기 (파이썬)

@김백호2025. 5. 6. 19:06

최종 코드

import sys
from collections import deque
input=sys.stdin.readline

rectangle=[list(map(int,input().split())) for _ in range(4)]
characterX,characterY=map(int,input().split())
itemX,itemY=map(int,input().split())
dx = [0, 1, -1, 0]
dy = [-1, 0, 0, 1]

maps = [[0] * 101 for _ in range(101)]
me = [[0] * 101 for _ in range(101)]

for i in range(len(rectangle)):
    l_d_x, l_d_y = rectangle[i][0]*2, rectangle[i][1]*2
    r_u_x, r_u_y = rectangle[i][2]*2, rectangle[i][3]*2
    for y in range(l_d_y,r_u_y+1):
        if maps[y][l_d_x]==1 or maps[y][l_d_x]==0:
            maps[y][l_d_x]=1
        if maps[y][r_u_x]==1 or maps[y][r_u_x]==0:
            maps[y][r_u_x]=1

    for x in range(l_d_x,r_u_x+1):
        if maps[l_d_y][x]==1 or maps[l_d_y][x]==0:
            maps[l_d_y][x]=1
        if maps[r_u_y][x]==1 or maps[r_u_y][x]==0:
            maps[r_u_y][x]=1

    for y in range(l_d_y+1,r_u_y):
        for x in range(l_d_x+1,r_u_x):
            maps[y][x]=2


# for row in reversed(maps[:r_u_y+2]):
#     print(''.join(str(cell) for cell in row[:r_u_x+2]))
queue=deque()
queue.append((characterY*2,characterX*2))
maps[characterY*2][characterX*2]=0
while queue:
    y,x = queue.popleft()
    for i in range(4):
        nx = x+dx[i]
        ny = y+dy[i]
        if 0 <= nx <= 100 and 0 <= ny <= 100 and maps[ny][nx] == 1:
            maps[ny][nx]=0
            queue.append((ny,nx))
            me[ny][nx]=me[y][x]+1
            if ny==itemY*2 and nx== itemX*2:
                queue.clear()
                break

print(me[itemY*2][itemX*2]//2)





# for row in reversed(me[:r_u_y+3]):
#     print(''.join(str(cell) for cell in row[:r_u_x+3]))

maps에 사각형 테두리를 표현하려 했다.

갈 수 있는 곳은 1 가지 못하는 곳은 2. 하지만 사각형의 좌표를 그대로 매핑하면 회전을 해야하는 상황에서 직진을 하는 상황이 발생했다.

따라서 모든 좌표를 2배하여 매핑하였다.

그러니 경로가 명확하게 나타났다.

그 이후로는 미로찾기와 비슷하게 bfs를 활용하여 풀이 하였다.

김백호
@김백호 :: 낭만 코딩

공감하셨다면 ❤️ 구독도 환영합니다! 🤗

목차