-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_prog_compute_value.py
53 lines (46 loc) · 1.58 KB
/
dynamic_prog_compute_value.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
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 10 15:05:55 2020
@author: cenic
"""
# ----------
# User Instructions:
#
# Create a function compute_value which returns
# a grid of values. The value of a cell is the minimum
# number of moves required to get from the cell to the goal.
#
# If a cell is a wall or it is impossible to reach the goal from a cell,
# assign that cell a value of 99.
# ----------
grid = [[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0]]
goal = [len(grid)-1, len(grid[0])-1]
cost = 1 # the cost associated with moving from a cell to an adjacent one
delta = [[-1, 0], # go up
[0, -1], # go left
[1, 0], # go down
[0, 1]] # go right
delta_name = ['^', '<', 'v', '>']
def compute_value(grid, goal, cost):
value = [[99 for j in grid[0]]for i in grid]
visitednodes = [goal]
visnodesset = [goal]
value[goal[0]][goal[1]] = 0
while visitednodes:
currentnode = visitednodes.pop(0)
for mov in delta:
x2 = mov[0] + currentnode[0]
y2 = mov[1] + currentnode[1]
if x2 >= 0 and y2 >= 0 and x2 < len(grid) and y2 < len(grid[0]):
if [x2, y2] not in visnodesset and grid[x2][y2] == 0:
visnodesset.append([x2, y2])
visitednodes.append([x2, y2])
value[x2][y2] = value[currentnode[0]][currentnode[1]]+cost
# ----------------------------------------
# insert code below
# ----------------------------------------
return value