-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccc_2010_J5.py
52 lines (48 loc) · 1.69 KB
/
ccc_2010_J5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
start = [int(_) for _ in input().split()]
end = [int(_) for _ in input().split()]
start = tuple(x for x in start)
end = tuple(x for x in end)
length = list(range(1, 9))
def possible_moves(x, y):
moves = []
hop_right = True if x + 2 in length else False
hop_left = True if x - 2 in length else False
hop_up = True if y + 2 in length else False
hop_down = True if y - 2 in length else False
hopsHorizontal = [(hop_right, x + 2), (hop_left, x - 2)]
hopsVertical = [(hop_up, y + 2), (hop_down, y - 2)]
for i in hopsHorizontal:
if y - 1 in length and i[0] == True:
moves.append((i[1], y - 1))
if y + 1 in length and i[0]:
moves.append((i[1], y + 1))
for i in hopsVertical:
if x - 1 in length and i[0]:
moves.append((x - 1, i[1]))
if x + 1 in length and i[0]:
moves.append((x + 1, i[1]))
return moves
def horse(start, end):
search = []
next_values = []
distance = 1
x, y = start
current = start
points_to_check = [i for i in possible_moves(x, y)]
points_checked = [tuple(i for i in current)]
while(True):
if current == end:
return 0
if end in points_to_check:
return distance
else:
for i in points_to_check:
x, y = i
search = possible_moves(x, y)
points_checked.append(tuple(x for x in i))
for i in search:
if i not in points_checked:
next_values.append(tuple(x for x in i))
points_to_check = set(next_values)
distance += 1
print(horse(start, end))