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 38 39 40 41
| import queue x1,y1=map(int,input().split()) x2,y2=map(int,input().split())
grid=[[-1 for _ in range(20)] for _ in range(20)] dist=[[0 for _ in range(20)] for _ in range(20)]
dx=[1,1,-1,-1,2,2,-2,-2,2,2,-2,-2] dy=[2,-2,2,-2,1,-1,1,-1,2,-2,2,-2] q=queue.Queue()
def bfs(x,y): q.put([x-1,y-1]) grid[x-1][y-1]=0 dist[x-1][y-1]=1 while(not q.empty()): now = q.get() for i in range(12): a,b=now[0]+dx[i],now[1]+dy[i] if(a<0 or b<0 or a>19 or b>19): continue if(dist[a][b] != 0): continue grid[a][b]=grid[now[0]][now[1]]+1 dist[a][b]=1 q.put([a,b]) return grid
ans_1=bfs(x1,y1)
while(not q.empty()): q.get() grid=[[-1 for _ in range(20)] for _ in range(20)] dist=[[0 for _ in range(20)] for _ in range(20)] ans_2=bfs(x2,y2) print(ans_1[0][0]) print(ans_2[0][0])
|