Algorithm Problem/Python
[python] ๋ฐฑ์ค - 7562. ๋์ดํธ์ ์ด๋
deo2kim
2020. 10. 2. 08:00
๋ฐ์ํ
๐ค๋ฌธ์ ํด๊ฒฐ
-
S2 | BFS
BFS๋ก ๋ค ๋์์ฃผ๋ค๊ฐ ๋์ฐฉ์ง๋ฅผ ๋ง๋๋ฉด ๋!
๐ป์์ค ์ฝ๋
from collections import deque
def bfs():
q = deque()
q.append((start_x, start_y))
while q:
cx, cy = q.popleft()
if cx == end_x and cy == end_y:
print(chess[cx][cy])
return
for k in range(len(dx)):
nx = cx + dx[k]
ny = cy + dy[k]
if 0 <= nx < N and 0 <= ny < N and chess[nx][ny] == '':
chess[nx][ny] = chess[cx][cy] + 1
if nx == end_x and ny == end_y:
print(chess[nx][ny])
return
q.append((nx, ny))
for tc in range(int(input())):
N = int(input())
chess = [[''] * N for _ in range(N)]
start_x, start_y = map(int, input().split())
end_x, end_y = map(int, input().split())
chess[start_x][start_y] = 0
dx, dy = [-2, -1, 1, 2, 2, 1, -1, -2], [1, 2, 2, 1, -1, -2, -2, -1]
bfs()
๐๋ฌธ์ ํ์ธ
์ถ์ฒ: BACKJOON ONLINE JUDGE
๋งํฌ: https://www.acmicpc.net/problem/7562
7562๋ฒ: ๋์ดํธ์ ์ด๋
์ฒด์คํ ์์ ํ ๋์ดํธ๊ฐ ๋์ฌ์ ธ ์๋ค. ๋์ดํธ๊ฐ ํ ๋ฒ์ ์ด๋ํ ์ ์๋ ์นธ์ ์๋ ๊ทธ๋ฆผ์ ๋์์๋ค. ๋์ดํธ๊ฐ ์ด๋ํ๋ ค๊ณ ํ๋ ์นธ์ด ์ฃผ์ด์ง๋ค. ๋์ดํธ๋ ๋ช ๋ฒ ์์ง์ด๋ฉด ์ด ์นธ์ผ๋ก ์ด๋ํ ์ ๏ฟฝ๏ฟฝ
www.acmicpc.net
๋ฐ์ํ