-
Notifications
You must be signed in to change notification settings - Fork 0
/
walking_robot.py
61 lines (45 loc) · 1.46 KB
/
walking_robot.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
53
54
55
56
57
58
59
60
61
matrix = [['46B', 'E59', 'EA', 'C1F', '45E', '63'],
['899', 'FFF', '926', '7AD', 'C4E', 'FFF'],
['E2E', '323', '6D2', '976', '83F', 'C96'],
['9E9', 'A8B', '9C1', '461', 'F74', 'D05'],
['EDD', 'E94', '5F4', 'D1D', 'D03', 'DE3'],
['89', '925', 'CF9', 'CA0', 'F18', '4D2']]
print 'Matrix:'
for i in range(6):
for j in range(6):
print "%4s(%4s)" % (matrix[i][j], int(matrix[i][j], 16)),
print ''
print '-------------------------------'
mincost = [[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]
mincost[0][0] = int(matrix[0][0], 16)
for i in range(1, 6):
mincost[0][i] = mincost[0][i-1] + int(matrix[0][i], 16)
for i in range(1, 6):
mincost[i][0] = mincost[i-1][0] + int(matrix[i][0], 16)
for i in range(1, 6):
for j in range(1, 6):
mincost[i][j] = min(mincost[i-1][j],
mincost[i][j-1]) + int(matrix[i][j], 16)
print 'Minimun cost from left/top to right/down: %s' % mincost[5][5]
path = ''
while True:
if i == 0 and j == 0:
break
if i == 0:
j -= 1
path += 'r,'
elif j == 0:
i -= 1
path += 'd,'
elif mincost[i-1][j] < mincost[i][j-1]:
i -= 1
path += 'd,'
elif mincost[i-1][j] > mincost[i][j-1]:
j -= 1
path += 'r,'
print 'Path: %s' % path[::-1][1:]