Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- start1, start2 = input().split()
- x1, y1 = start1
- x2, y2 = start2
- x1 = ord(x1) - ord('a')
- x2 = ord(x2) - ord('a')
- y1 = int(y1) - 1
- y2 = int(y2) - 1
- visited = set()
- visited.add((x1, y1, x2, y2))
- q = [(x1, y1, x2, y2, 0)]
- res = -1
- while q:
- x1, y1, x2, y2, steps = q.pop(0)
- if x1 == x2 and y1 == y2:
- res = steps
- break
- for xp1, yp1 in [(x1-2, y1+1), (x1-2, y1-1), (x1-1, y1-2), (x1-1, y1+2),
- (x1+1, y1-2), (x1+1, y1+2), (x1+2, y1-1), (x1+2, y1+1)]:
- if 0 <= xp1 < 8 and 0 <= yp1 < 8:
- for xp2, yp2 in [(x2-2, y2+1), (x2-2, y2-1), (x2-1, y2-2), (x2-1, y2+2),
- (x2+1, y2-2), (x2+1, y2+2), (x2+2, y2-1), (x2+2, y2+1)]:
- if 0 <= xp2 < 8 and 0 <= yp2 < 8:
- if (xp1, yp1, xp2, yp2) not in visited:
- visited.add((xp1, yp1, xp2, yp2))
- q.append((xp1, yp1, xp2, yp2, steps+1))
- print(res)
Advertisement
Add Comment
Please, Sign In to add comment