문제링크 : https://school.programmers.co.kr/learn/courses/30/lessons/87694
from collections import deque
def solution(rectangle, chX, chY, itX, itY):
dx, dy = [0,0,-1,1], [1,-1,0,0]
# 테두리 그리기
graph = [[-1] * 102 for _ in range(102)]
for r in rectangle:
x1,y1,x2,y2 = map(lambda x:x*2, r) # 두배
for i in range(x1,x2+1):
for j in range(y1, y2+1):
if x1 < i < x2 and y1 < j < y2: #테두리 내부는 0
graph[i][j] = 0
elif graph[i][j] != 0 : # 다른 사각형의 테두리 내부면 0
graph[i][j] = 1
q = deque()
q.append((2*chX,2*chY))
visited = [[0] * 101 for _ in range(101)]
visited[2*chX][2*chY] = 1
cnt = 0
while q:
cnt += 1
for _ in range(len(q)):
x,y = q.popleft()
if x == 2*itX and y == 2*itY:
return cnt//2
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if graph[nx][ny] == 1 and not visited[nx][ny]:
visited[nx][ny] = 1
q.append((nx,ny))