Skip to content

Commit 631f627

Browse files
committed
[BOJ] 7562 나이트의 이동(S1)
1 parent eaaf7a2 commit 631f627

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

김지호/2주차/260106.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
from collections import defaultdict
3+
from collections import deque
4+
5+
sys.stdin = open("../input.txt",'r')
6+
7+
Testcase = int(input())
8+
9+
answers = []
10+
11+
def solution():
12+
N = int(input())
13+
sRow, sCol = map(int,input().split(" "))
14+
eRow, eCol = map(int,input().split(" "))
15+
16+
drow = [-2,-1,1,2,2,1,-1,-2]
17+
dcol = [1,2,2,1,-1,-2,-2,-1]
18+
19+
queue= deque()
20+
visited = [[False] * N for _ in range(N)]
21+
22+
queue.append([sRow,sCol,0]) # [row, col, 이동칸 수]
23+
visited[sRow][sCol] = True
24+
25+
while(len(queue) != 0):
26+
node = queue.popleft()
27+
cRow, cCol, move = node
28+
29+
# 종료 조건
30+
if(cRow == eRow and cCol == eCol):
31+
return move
32+
33+
for i in range(8):
34+
nRow = cRow + drow[i]
35+
nCol = cCol + dcol[i]
36+
37+
if(0 <= nRow < N and 0 <= nCol < N and visited[nRow][nCol] == False):
38+
queue.append([nRow,nCol, move + 1])
39+
visited[nRow][nCol] = True
40+
41+
42+
43+
for _ in range(Testcase):
44+
print(solution())
45+
46+
47+

0 commit comments

Comments
 (0)