-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path003a_shortest_path_of_the_king.py
51 lines (48 loc) · 1.38 KB
/
003a_shortest_path_of_the_king.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
#! /usr/bin/python
import sys
def shortest_path(x1, y1, x2, y2):
path = []
if x1 == x2:
offset = y1 - y2
while offset != 0:
if offset < 0:
path.append('U')
offset += 1
else:
path.append('D')
offset -= 1
elif y1 == y2:
offset = x1 - x2
while offset != 0:
if offset < 0:
path.append('R')
offset += 1
else:
path.append('L')
offset -= 1
else:
sgn = 1.0 * (y1 - y2) / (x1 - x2)
if sgn > 0:
if y1 > y2:
path.append('LD')
path.extend(shortest_path(x1-1, y1-1, x2, y2))
else:
path.append('RU')
path.extend(shortest_path(x1+1, y1+1, x2, y2))
else:
if y1 > y2:
path.append('RD')
path.extend(shortest_path(x1+1, y1-1, x2, y2))
else:
path.append('LU')
path.extend(shortest_path(x1-1, y1+1, x2, y2))
return path
if __name__ == '__main__':
a = sys.stdin.readline()
b = sys.stdin.readline()
x1, y1 = ord(a[0]) - ord('a') + 1, int(a[1])
x2, y2 = ord(b[0]) - ord('a') + 1, int(b[1])
path = shortest_path(x1, y1, x2, y2)
print len(path)
for i in path:
print i