No es el mas efectivo pero es trabajo honesto :V
import math
MOVEMENTS = [
(2, 1), (1, 2), (-2, 1), (-1, 2),
(2, -1), (1, -2), (-2, -1), (-1, -2)
]
def distance_between_points(x1, y1, x2, y2):
return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
def move_point(x: int, y: int, search_point: tuple, previous_point, previous_distance=0):
list_possible_moves = []
for movement_index in range(0, len(MOVEMENTS)):
next_x = x + MOVEMENTS[movement_index][0]
next_y = y + MOVEMENTS[movement_index][1]
distance = distance_between_points(next_x, next_y, search_point[0], search_point[1])
if distance == 0:
return None
if (next_x >= search_point[0] and next_y >= search_point[1]) \
or (distance > distance_between_points(0, 0, search_point[0], search_point[1])):
continue
list_possible_moves.append([next_x, next_y, previous_distance + distance, previous_point + [(next_x, next_y)]])
list_possible_moves.sort(key=lambda x: x[2])
return list_possible_moves
def horse_steps(x, y):
initial = [[0, 0]]
jumps = 1
while jumps < 1000:
jumps = jumps + 1
next_step = []
for step in initial:
response = move_point(x=step[0], y=step[1], search_point=(x, y), previous_point=[(step[0], step[1])])
if response is None:
return jumps
next_step = next_step + response
if next_step is None:
print('No more steps')
break
initial = next_step
if __name__ == '__main__':
assert horse_steps(2, 1) == 2
assert horse_steps(5, 5) == 5
assert horse_steps(5, 0) == 4
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?