-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDP_robot_in_grid_dp.py
90 lines (79 loc) · 2.22 KB
/
DP_robot_in_grid_dp.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Robot in a Grid Dynamic Programming (CtCI/Chapter_8/8.2)
'''
Imagine a bot sitting on the upper left corner of grid with r rows and c columns.
The robot can only move in 2 directions, right and down, but certain cells are "off limits" such that the robot cannot step on tgem.
Design an algorithm to fund a path for the robot from the top left to bottom right
'''
# Setup
'''
Grid
1: bad block
2: bottom right
Path: store path string
Dictionary:
keys: location of visited nodes (tuples (row, col)) as keys (immutable)
values: boolean
'''
# Idea
'''
Start from top left of the grid
Have row, col, and path as parameters of recurson function
Have a recursion:
Check if robot goes out of grid or hits bad block
If robot ends up at bottom right, return path
Else
Check if the current node is visited
Else
Mark current node as visited node
Increment col by 1 for going to the right and add 'right' to path
Increment row by 1 for going down and add 'down' to path
'''
# Implement
import time
start_time = 0
grid = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 1, 1],
[0, 0, 0, 2]
]
path_result = ''
visited_nodes = {}
def move(row, col, path):
global path_result
global visited_nodes
if (row > 3 or col > 3) or (grid[row][col] == 1):
return
elif grid[row][col] == 2:
path_result = path
return
else:
# go right
if (row,col) in visited_nodes:
# print('row, col in dict: ')
# print(visited_nodes)
return
else:
# print('row, col NOT in dict: ')
visited_nodes[(row,col)] = True
# print(visited_nodes)
# print(visited_nodes[(row, col)])
# go right
move(row, col+1, path + ' right')
# go down
move(row+1, col, path + ' down')
def start():
global start_time
start_time = time.time()
move(0, 1, 'right')
if not path_result == '':
return path_result
move(1, 0, 'down')
if not path_result == '':
return path_result
return 'No path found'
result = start()
print(result)
end_time = time.time()
print('total time: ')
print(end_time - start_time)