Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. from collections import deque
  2. import sys
  3.  
  4. n, q = int(input()), deque()
  5. x1, y1 = map(int, input().split())
  6. x1, y1 = x1-1, y1-1
  7. x2, y2 = map(int, input().split())
  8. x2, y2 = x2-1, y2-1
  9. x = [-2, -2, -1, -1, 1, 1, 2, 2]
  10. y = [-1, 1, -2, 2, -2, 2, -1, 1]
  11. stol = [0]*n
  12. for i in range(n):
  13. stol[i] = [1000000000]*n
  14. stol[x1][y1] = 0
  15. q.appendleft([x1, y1])
  16. while q:
  17. front = q.popleft()
  18. i = front[0]
  19. j = front[1]
  20. for l in range(8):
  21. k = (i + x[l])
  22. t = (j + y[l])
  23. if (k >= 0 and k < n):
  24. if t >= 0 and t < n:
  25. if stol[k][t] > stol[i][j] + 1:
  26. stol[k][t] = stol[i][j] + 1
  27. q.appendleft([k, t])
  28. print(stol[x2][y2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement