๋ฐ์ํ
๐ค๋ฌธ์ ํด๊ฒฐ
- ํ
๋๋ฆฌ ๊ทธ๋ฆฌ๊ธฐ
- 2์ฐจ์ ๋ฐฐ์ด ๋ง๋ค์ด ๋๊ณ
- ํ ๋๋ฆฌ๋ 1, ๋ด๋ถ๋ 0์ผ๋ก ํ์
- ํ ๋๋ฆฌ์ ๋ด๋ถ๊ฐ ๊ฒน์น ๊ฒฝ์ฐ 0์ผ๋ก ํ์
- ์ ๊ทธ๋ฆผ์ด ์์์ ๋ฐฉํฅ๊ณผ ๋ค๋ฅธ ์ด์ ๋ ์ขํ๊ณ๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ
- ๊ทธ๋ฆฌ๊ณ 2๋ฐฐ๋ฅผ ์ฃผ๊ณ ํ
๋๋ฆฌ๋ฅผ ์ก์๋ค
- ๊ทธ ์ด์ ๋ 2๋ฐฐ๋ฅผ ์์ฃผ๋ฉด ๊ธธ์ด ์๋์ฌ๋ 1์นธ ์ฐจ์ด๊ฐ ๋ ์ ์๊ธฐ ๋๋ฌธ์ ๊ฒฝ๋ก๊ฐ ๋์ด ๋ฒ๋ฆฐ๋ค.
- ๊ธธ ์ฐพ๊ธฐ๋ BFS ๋ก ์ต๋จ๊ฑฐ๋ฆฌ ์ฐพ์์ฃผ๋ฉด ๋
๐ป์์ค ์ฝ๋
from collections import deque
def solution(rectangle, characterX, characterY, itemX, itemY):
answer = 0
MAX = 102 # ๋๋ฐฐ๋ก ๋๋ฆฌ๊ธฐ ๋๋ฌธ์ ์ต๋ 102
# ํ
ํฌ๋ฆฌ ๊ทธ๋ฆฌ๊ธฐ
field = [[5] * MAX for _ in range(MAX)] # 5๋ ๋งจ์ฒ์ ๋
for rec in rectangle:
x1, y1, x2, y2 = map(lambda x: x * 2, rec)
for i in range(x1, x2 + 1):
for j in range(y1, y2 + 1):
if x1 < i < x2 and y1 < j < y2: # ๋ด๋ถ์ผ ๋
field[i][j] = 0
elif field[i][j] != 0: # ํ
๋๋ฆฌ์ผ ๋ ๊ทธ๋ฆฌ๊ณ ์ด๋ฏธ ๋ด๋ถ๊ฐ ์๋ ๋
field[i][j] = 1 # ํ
ํฌ๋ฆฌ๋ ๋ด๋ถ๋ ๊ฒน์น๋ฉด ๊ทธ๊ฑด ๋ด๋ถ
# ๊ธธ ์ฐพ๊ธฐ (์ต๋จ๊ฑฐ๋ฆฌ๋ BFS)
q = deque()
q.append([characterX * 2, characterY * 2])
visited = [[0] * MAX for _ in range(MAX)]
visited[characterX * 2][characterY * 2] = 1
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
while q:
x, y = q.popleft()
if x == itemX * 2 and y == itemY * 2:
answer = (visited[x][y] - 1) // 2
break
for k in range(4):
nx = x + dx[k]
ny = y + dy[k]
if visited[nx][ny] == 0 and field[nx][ny] == 1:
q.append([nx, ny])
visited[nx][ny] = visited[x][y] + 1
return answer
๐๋ฌธ์ ํ์ธ
์ถ์ฒ: ํ๋ก๊ทธ๋๋จธ์ค
๋งํฌ: ์์ดํ ์ค๊ธฐ
๋ฐ์ํ