Skip to content

Instantly share code, notes, and snippets.

@asunar
Last active October 5, 2017 06:54
Show Gist options
  • Save asunar/f2bec8964c5be4252979ed3d7e237524 to your computer and use it in GitHub Desktop.
Save asunar/f2bec8964c5be4252979ed3d7e237524 to your computer and use it in GitHub Desktop.
# ----------
# User Instructions:
#
# Define a function, search() that returns a list
# in the form of [optimal path length, row, col]. For
# the grid shown below, your function should output
# [11, 4, 5].
#
# If there is no valid path from the start point
# to the goal, your function should return the string
# 'fail'
# ----------
# Grid format:
# 0 = Navigable space
# 1 = Occupied space
grid = [[0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 1, 0]]
init = [0, 0]
goal = [len(grid)-1, len(grid[0])-1]
cost = 1
delta = [[-1, 0], # go up
[ 0,-1], # go left
[ 1, 0], # go down
[ 0, 1]] # go right
delta_name = ['^', '<', 'v', '>']
def search(grid,init,goal,cost, g_val = 0):
# ----------------------------------------
# insert code here
# ----------------------------------------
if g_val == 0:
init.insert(g_val, 0);
open = init
print("initial open list:")
print(open)
print("---")
#return path
#search(grid, init, goal, cost)
def isOpenCell(x, y, grid):
max_y = len(grid)
max_x = len(grid[0])
#print(str(max_y) + " x " + str(max_x) + " grid")
isInsideGrid = (x >= 0 and x <=max_y - 1) and (y >= 0 and y <= max_x -1)
#print(str(x) + "," + str(y))
#print("isInsideGrid: " + str(isInsideGrid))
if(isInsideGrid == False):
#print("---")
return False
isBlocked = grid[x][y] == 1
#print("isBlocked: " + str(isBlocked))
#print("---")
return isBlocked == False
assert isOpenCell(-1,0,grid) == False, 'NOT Inside'
assert isOpenCell(0,-1,grid) == False, 'NOT Inside'
assert isOpenCell(0,0,grid) == True, 'Inside'
assert isOpenCell(0,5,grid) == True, 'Inside'
assert isOpenCell(6,0,grid) == False, 'NOT Inside'
assert isOpenCell(0,4,grid) == True, 'Inside'
assert isOpenCell(0,2,grid) == False, 'NOT Open (BLOCKED)'
assert isOpenCell(1,2,grid) == False, 'NOT Open (BLOCKED)'
assert isOpenCell(2,4,grid) == False, 'NOT Open (BLOCKED)'
assert isOpenCell(3,2,grid) == False, 'NOT Open (BLOCKED)'
assert isOpenCell(3,3,grid) == False, 'NOT Open (BLOCKED)'
assert isOpenCell(3,4,grid) == False, 'NOT Open (BLOCKED)'
assert isOpenCell(4,4,grid) == False, 'NOT Open (BLOCKED)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment