Advertisement
Nenogzar

Най-дългият път

Feb 12th, 2024
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1.  
  2.     total_moves = 0  # Общ брой на движенията
  3.     total_exits = 0  # Общ брой на възможните изходи
  4.     longest_path = []  # Най-дългият път
  5.  
  6.     while next_free:
  7.         x, y = next_free.pop(0)
  8.         moves = 0  # Брой на движенията за текущата позиция
  9.  
  10.         # Проверка за движение наляво
  11.         if position[0] == x and position[1] - y == 1:
  12.             moves += 1
  13.         # Проверка за движение надясно
  14.         elif position[0] == x and y - position[1] == 1:
  15.             moves += 1
  16.         # Проверка за движение надолу
  17.         elif x - position[0] == 1 and position[1] == y:
  18.             moves += 1
  19.         # Проверка за движение нагоре
  20.         elif position[0] - x == 1 and position[1] == y:
  21.             moves += 1
  22.  
  23.         # Обновяване на броя движения за текущата позиция
  24.         total_moves += moves
  25.  
  26.         # Проверка дали текущата позиция е изход от лабиринта
  27.         if position[0] == 0 or position[0] == (len(maze) - 1) or position[1] == 0 or position[1] == (len(maze[0]) - 1):
  28.             total_exits += 1
  29.  
  30.             # Запазване на текущия път, ако е по-дълъг от предишния
  31.             if moves > len(longest_path):
  32.                 longest_path = [(position[0], position[1])] + [(x, y) for x, y in next_free]
  33.  
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement