๋ฌธ์
์ฒด์คํ ์์ ํ ๋์ดํธ๊ฐ ๋์ฌ์ ธ ์๋ค. ๋์ดํธ๊ฐ ํ ๋ฒ์ ์ด๋ํ ์ ์๋ ์นธ์ ์๋ ๊ทธ๋ฆผ์ ๋์์๋ค. ๋์ดํธ๊ฐ ์ด๋ํ๋ ค๊ณ ํ๋ ์นธ์ด ์ฃผ์ด์ง๋ค. ๋์ดํธ๋ ๋ช ๋ฒ ์์ง์ด๋ฉด ์ด ์นธ์ผ๋ก ์ด๋ํ ์ ์์๊น?
์ ๋ ฅ
์
๋ ฅ์ ์ฒซ์งธ ์ค์๋ ํ
์คํธ ์ผ์ด์ค์ ๊ฐ์๊ฐ ์ฃผ์ด์ง๋ค.
๊ฐ ํ
์คํธ ์ผ์ด์ค๋ ์ธ ์ค๋ก ์ด๋ฃจ์ด์ ธ ์๋ค. ์ฒซ์งธ ์ค์๋ ์ฒด์คํ์ ํ ๋ณ์ ๊ธธ์ด l(4 ≤ l ≤ 300)์ด ์ฃผ์ด์ง๋ค. ์ฒด์คํ์ ํฌ๊ธฐ๋ l × l์ด๋ค. ์ฒด์คํ์ ๊ฐ ์นธ์ ๋ ์์ ์ {0, ..., l-1} × {0, ..., l-1}๋ก ๋ํ๋ผ ์ ์๋ค. ๋์งธ ์ค๊ณผ ์
์งธ ์ค์๋ ๋์ดํธ๊ฐ ํ์ฌ ์๋ ์นธ, ๋์ดํธ๊ฐ ์ด๋ํ๋ ค๊ณ ํ๋ ์นธ์ด ์ฃผ์ด์ง๋ค.
์ถ๋ ฅ
๊ฐ ํ ์คํธ ์ผ์ด์ค๋ง๋ค ๋์ดํธ๊ฐ ์ต์ ๋ช ๋ฒ๋ง์ ์ด๋ํ ์ ์๋์ง ์ถ๋ ฅํ๋ค.
์์ ์ ๋ ฅ
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
์์ ์ถ๋ ฅ
5
28
0
ํด๊ฒฐ ๋ฐฉ๋ฒ
from collections import deque
t = int(input())
def checkValid(y, x, l):
return 0 <= y < l and 0 <= x <l
for test_case in range(1, t+1):
l = int(input()) # ์ฒด์คํ์ ๊ธธ์ด
current_pos = list(map(int, input().split()))
destination = list(map(int, input().split()))
movements = [[+2, +1], [-2, +1], [+1, +2], [-1, +2],
[+2, -1], [-2, -1], [+1, -2], [-1, -2]]
queue = deque()
visited = [[0 for _ in range(l)] for _ in range(l)]
queue.append(current_pos)
while queue:
y, x = queue.popleft()
if [y, x] == destination:
print(visited[y][x])
break
for move in movements:
ny, nx = y + move[0], x + move[1]
if checkValid(ny, nx, l) and not visited[ny][nx]:
visited[ny][nx] = visited[y][x] + 1
queue.append((ny, nx))
'๐์ฝ๋ฉํ ์คํธ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 2166 - ๋ค๊ฐํ์ ๋ฉด์ (0) | 2024.11.25 |
---|---|
[๋ฐฑ์ค] 19598 - ์ต์ ํ์์ค ๊ฐ์ (0) | 2024.11.24 |
[๋ฐฑ์ค] 15486 - ํด์ฌ 2 (0) | 2024.11.19 |
[๋ฐฑ์ค] 4485 - ๋ น์ ์ท ์ ์ ์ ๊ฐ ์ ค๋ค์ง? (0) | 2024.11.18 |
[๋ฐฑ์ค] 22988 - ์ฌํ์ฉ ์บ ํ์ธ (0) | 2024.11.14 |