Advertisement
Guest User

neighbours()

a guest
Jan 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. def neighbours(maze, pos):
  2.     """
  3.    Функция возвращает возможные направления движения в лабиринте
  4.    относительно текущей позиции
  5.  
  6.    :param maze: Лабиринт
  7.    :param pos: Текущая позиция в лабиринте (строка, столбец)
  8.    :return: Список возможных позиций
  9.  
  10.    >>> neighbours([
  11.    [False, False, False, False, False, False, False],
  12.    [False, True, True, False, True, None, False],
  13.    [False, False, True, False, True, False, False],
  14.    [False, True, True, False, True, True, False],
  15.    [False, True, False, False, False, True, False],
  16.    [False, True, True, True, True, True, False],
  17.    [False, False, False, False, False, False, False]
  18.    ], (1,1))
  19.    [[0,1]]
  20.    """
  21.     neighbours = []
  22.  
  23.     row, col = pos
  24.     nrows, ncols = shape(maze)
  25.  
  26.     if 0 < row < nrows and 0 < col < ncols:  # Рамку лабиринта мы не проверяем.
  27.  
  28.         if maze[row][col - 1] is True:  # Есть свободная позиция слева
  29.             neighbours.append((-1, 0))
  30.  
  31.         if maze[row][col + 1] is True:  # Есть свободная позиция справа
  32.             neighbours.append((1, 0))
  33.  
  34.         if maze[row - 1][col] is True:  # Есть свободная позиция сверху
  35.             neighbours.append((0, 1))
  36.  
  37.         if maze[row + 1][col] is True:  # Есть свободная позиция снизу
  38.             neighbours.append((0, -1))
  39.  
  40.     return neighbours
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement