Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Converts string into coordinates
- def get_coordinates(pos_string):
- return (ord(pos_string[0]) - ord('a'), int(pos_string[1]) - 1)
- # Returns all the possible spaces or neighbours where the piece can move to.
- def get_neighbours(position):
- x_movemens = [2, 2, -2, -2, 1, -1, 1, -1]
- y_movements = [1, -1, 1, -1, 2, 2, -2, -2]
- neighbours = []
- for x, y in zip(x_movemens, y_movements):
- if 0 <= position[0] + x <= 7 and 0 <= position[1] + y <= 7:
- neighbours.append((position[0] + x, position[1] + y))
- return neighbours
- def distance_finder(source, sink):
- visited = [False]*64
- distance_list = [0]*64
- node_queue = []
- node_queue.append(source)
- # source is a tuple pair. Multiplying 8 with the first part and then adding the second part
- # gives the appropriate index of that pair in the visited list.
- visited[source[0] * 8 + source[1]] = True
- while node_queue:
- current_node = node_queue.pop(0)
- for neighbor in get_neighbours(current_node):
- if not visited[neighbor[0]*8 + neighbor[1]]:
- node_queue.append(neighbor)
- visited[neighbor[0] * 8 + neighbor[1]] = True
- distance_list[neighbor[0]*8 + neighbor[1]
- ] = distance_list[current_node[0] * 8 + current_node[1]] + 1
- return distance_list[sink[0]*8 + sink[1]]
- test = int(input())
- for t in range(test):
- pos1, pos2 = map(str, input().split())
- print(distance_finder(get_coordinates(pos1), get_coordinates(pos2)))
Advertisement
Add Comment
Please, Sign In to add comment