๋ฐ์ํ
๐ค๋ฌธ์ ํด๊ฒฐ
-
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
๋ฐ์ํ
'Algorithm Problem > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python] ๋ฐฑ์ค - 11055. ๊ฐ์ฅ ํฐ ์ฆ๊ฐ ๋ถ๋ถ ์์ด (0) | 2020.10.04 |
---|---|
[python] ๋ฐฑ์ค - 11722. ๊ฐ์ฅ ๊ธด ๊ฐ์ํ๋ ๋ถ๋ถ ์์ด (0) | 2020.10.03 |
[python] ๋ฐฑ์ค - 4948. ๋ฒ ๋ฅดํธ๋ ๊ณต์ค (0) | 2020.10.01 |
[python] ๋ฐฑ์ค - 16234. ์ธ๊ตฌ ์ด๋(์ผ์ฑ SW ์ญ๋ ํ ์คํธ ๊ธฐ์ถ ๋ฌธ์ ) (2) | 2020.09.30 |
[python] ๋ฐฑ์ค - 1931. ํ์์ค ๋ฐฐ์ (0) | 2020.09.29 |