백준 7562 - 나이트의 이동


백준 7562 - 나이트의 이동

12345678910111213141516171819202122232425262728293031323334from collections import dequeimport sys def bfs(fr,to,cnt): q = deque() q.append((fr[1],fr[0],0)) visit = [[0] * l for _ in range(l)] while q: x, y, c = q.popleft() for dx, dy in dir: nx, ny = x + dx, y + dy if nx < 0 or nx >= l or ny < 0 or ny >= l: continue if visit[ny][nx] == 0: q.append((nx, ny, c+1)) visit[ny][nx] = 1 if nx == to[1] and ny == to[0]: cnt.append(c+1) return min(cnt) dir = [(-2,1),(-1,2),(1,2),(2,1),(-2,-1),(-1,-2),(2,-1),(1,-2)] for _ in range(int(sys.stdin.readline())): l = int(sys.stdin.readline()) fr ..........



원문링크 : 백준 7562 - 나이트의 이동