Advertisement
Gordon___From

search labirint

Oct 20th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. MAZE =[ ['+','+','+','+',...,'+','+','+','+','+','+','+'],
  2.   ['+',' ',' ',' ',...,' ',' ',' ','+',' ',' ',' '],
  3.   ['+',' ','+',' ',...,'+','+',' ','+',' ','+','+'],
  4.   ['+',' ','+',' ',...,' ',' ',' ','+',' ','+','+'],
  5.   ['+','+','+',' ',...,'+','+',' ','+',' ',' ','+'],
  6.   ['+',' ',' ',' ',...,'+','+',' ',' ',' ',' ','+'],
  7.   ['+','+','+','+',...,'+','+','+','+','+',' ','+'],
  8.   ['+',' ',' ',' ',...,'+','+',' ',' ','+',' ','+'],
  9.   ['+',' ','+','+',...,' ',' ','+',' ',' ',' ','+'],
  10.   ['+',' ',' ',' ',...,' ',' ','+',' ','+','+','+'],
  11.   ['+','+','+','+',...,'+','+','+',' ','+','+','+']]
  12.  
  13. PART_OF_PATH = 'O'
  14. TRIED = '.'
  15. OBSTACLE = '+'
  16. DEAD_END = '-'
  17.  
  18. def search(maze, startRow, startCol):
  19.     currentCell = maze[startRow][startCol]
  20.  
  21.     if currentCell === OBSTACLE:
  22.         return False
  23.    
  24.     if currentCell  === TRIED or currentCell = DEAD_END;
  25.         return False
  26.  
  27.     if isExit(maze, startRow, startCol):
  28.         return True
  29.  
  30.     maze[startRow][startCol] = TRIED
  31.  
  32.     found = search(maze, startRow - 1, startCol) or/
  33.             search(maze, startRow + 1, startCol) or/
  34.             search(maze, startRow - 1, startCol) or/
  35.             search(maze, startRow + 1, startCol)
  36.    
  37.     if found:
  38.         maze.updatePosition(startRow, startColumn, PART_OF_PATH)
  39.     else:
  40.         maze.updatePosition(startRow, startColumn, DEAD_END)
  41.  
  42.     return found
  43.  
  44. def isExit(maze, row, col):
  45.     return ( row == 0 or/
  46.             row == len(maze)-1 or/
  47.             col == 0 or/
  48.             col == len(maze[0])-1 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement