-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path나이트의이동.py
43 lines (27 loc) · 885 Bytes
/
나이트의이동.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from collections import deque
n = int(input())
dx = [1, 1, -1, -1, 2, 2, -2, -2]
dy = [2, -2, 2, -2, 1, -1, 1, -1]
for i in range(n):
m = int(input())
dis = [[0 for _ in range(m)] for i in range(m)]
ch = [[0 for _ in range(m)] for i in range(m)]
x, y = map(int, input().split())
goal_x, goal_y = map(int, input().split())
ch[x][y] = 1
dis[x][y] = 0
dq = deque()
dq.append((x, y))
while dq:
now_x, now_y = dq.popleft()
if now_x == goal_x and now_y == goal_y:
print(dis[now_x][now_y])
break
for j in range(8):
nx = now_x + dx[j]
ny = now_y + dy[j]
if 0<=nx<m and 0<=ny<m:
if ch[nx][ny] == 0:
ch[nx][ny] = 1
dis[nx][ny] = dis[now_x][now_y] + 1
dq.append((nx, ny))