serega1112

2 horses

Mar 23rd, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. start1, start2 = input().split()
  2.  
  3. x1, y1 = start1
  4. x2, y2 = start2
  5.  
  6. x1 = ord(x1) - ord('a')
  7. x2 = ord(x2) - ord('a')
  8.  
  9. y1 = int(y1) - 1
  10. y2 = int(y2) - 1
  11.  
  12. visited = set()
  13. visited.add((x1, y1, x2, y2))
  14. q = [(x1, y1, x2, y2, 0)]
  15.  
  16. res = -1
  17.  
  18. while q:
  19.     x1, y1, x2, y2, steps = q.pop(0)
  20.     if x1 == x2 and y1 == y2:
  21.         res = steps
  22.         break
  23.     for xp1, yp1 in [(x1-2, y1+1), (x1-2, y1-1), (x1-1, y1-2), (x1-1, y1+2),
  24.         (x1+1, y1-2), (x1+1, y1+2), (x1+2, y1-1), (x1+2, y1+1)]:
  25.         if 0 <= xp1 < 8 and 0 <= yp1 < 8:
  26.             for xp2, yp2 in [(x2-2, y2+1), (x2-2, y2-1), (x2-1, y2-2), (x2-1, y2+2),
  27.                 (x2+1, y2-2), (x2+1, y2+2), (x2+2, y2-1), (x2+2, y2+1)]:
  28.                 if 0 <= xp2 < 8 and 0 <= yp2 < 8:
  29.                     if (xp1, yp1, xp2, yp2) not in visited:
  30.                         visited.add((xp1, yp1, xp2, yp2))
  31.                         q.append((xp1, yp1, xp2, yp2, steps+1))
  32.  
  33. print(res)
Advertisement
Add Comment
Please, Sign In to add comment