Guest User

Untitled

a guest
Mar 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. def search(grid,init,goal,cost):
  2. # ----------------------------------------
  3. # insert code here
  4. # ----------------------------------------
  5. closed = [[0 for _ in range(len(grid[0]))] for bar in range(len(grid))]
  6. closed[init[0]][init[1]] = 1
  7.  
  8. x, y = init
  9. g = 0
  10. search = [[g, x, y]]
  11. path = [[' ' for col in range(len(grid[0]))] for row in range(len(grid))]
  12.  
  13. found = False
  14. done = False
  15. while not found and not done:
  16. if len(search) == 0:
  17. done = True
  18. return 'fail'
  19. else:
  20. search.sort(reverse = True)
  21. oldx, oldy = x, y
  22. g, x, y = search.pop()
  23. if oldx == x and oldy < y: # right
  24. path[oldx][oldy] = '>'
  25. elif oldx == x and oldy > y: # left
  26. path[oldx][oldy] = '<'
  27. elif oldy == y and oldx < x: # down
  28. path[oldx][oldy] = 'v'
  29. elif oldy == y and oldx > x: # up
  30. path[oldx][oldy] = '^'
  31. if [x,y] == goal:
  32. found = True
  33. path[x][y] = '*'
  34. else:
  35. for move in delta:
  36. tmpx = x + move[0]
  37. tmpy = y + move[1]
  38. if tmpx >= 0 and tmpx < len(grid) and tmpy >= 0 and tmpy < len(grid[0]):
  39. if (not closed[tmpx][tmpy]) and (not grid[tmpx][tmpy]):
  40. tmpg = g + cost
  41. search.append([tmpg, tmpx, tmpy])
  42. closed[tmpx][tmpy] = 1
  43.  
  44. return path
Add Comment
Please, Sign In to add comment