Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def neighbours(maze, pos):
- """
- Функция возвращает возможные направления движения в лабиринте
- относительно текущей позиции
- :param maze: Лабиринт
- :param pos: Текущая позиция в лабиринте (строка, столбец)
- :return: Список возможных позиций
- >>> neighbours([
- [False, False, False, False, False, False, False],
- [False, True, True, False, True, None, False],
- [False, False, True, False, True, False, False],
- [False, True, True, False, True, True, False],
- [False, True, False, False, False, True, False],
- [False, True, True, True, True, True, False],
- [False, False, False, False, False, False, False]
- ], (1,1))
- [[0,1]]
- """
- neighbours = []
- row, col = pos
- nrows, ncols = shape(maze)
- if 0 < row < nrows and 0 < col < ncols: # Рамку лабиринта мы не проверяем.
- if maze[row][col - 1] is True: # Есть свободная позиция слева
- neighbours.append((-1, 0))
- if maze[row][col + 1] is True: # Есть свободная позиция справа
- neighbours.append((1, 0))
- if maze[row - 1][col] is True: # Есть свободная позиция сверху
- neighbours.append((0, 1))
- if maze[row + 1][col] is True: # Есть свободная позиция снизу
- neighbours.append((0, -1))
- return neighbours
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement