Advertisement
Nenogzar

Untitled

Feb 12th, 2024
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.86 KB | None | 0 0
  1. # # Функция за намиране на началната позиция на "k" в лабиринта
  2. # def find_position(maze):
  3. #     position = []
  4. #     for row in range(len(maze)):
  5. #         for el in maze[row]:
  6. #             if el == 'k':
  7. #                 position.append(row)
  8. #                 position.append(maze[row].find('k'))
  9. #                 return position
  10. #
  11. # # Функция за намиране на свободните позиции (' ') в лабиринта
  12. # def next_free_spot(maze):
  13. #     free_spots = []
  14. #
  15. #     for row in range(len(maze)):
  16. #         for el in range(len(maze[row])):
  17. #             tmp = []
  18. #             if maze[row][el] == ' ':
  19. #                 tmp.append(row)
  20. #                 tmp.append(el)
  21. #                 free_spots.append(tmp)
  22. #
  23. #     return free_spots
  24. #
  25. # # Функция за намиране на пътя в лабиринта
  26. # def find_path(position, next_free, maze):
  27. #     moves = 0
  28. #
  29. #     while next_free:
  30. #         x1, x2 = next_free.pop(0)
  31. #
  32. #         # Проверка за движение наляво
  33. #         if position[0] == x1 and position[1] - x2 == 1:
  34. #             position = [x1, x2]
  35. #             moves += 1
  36. #         # Проверка за движение надясно
  37. #         elif position[0] == x1 and x2 - position[1] == 1:
  38. #             position = [x1, x2]
  39. #             moves += 1
  40. #         # Проверка за движение надолу
  41. #         elif x1 - position[0] == 1 and position[1] == x2:
  42. #             position = [x1, x2]
  43. #             moves += 1
  44. #         # Проверка за движение нагоре
  45. #         elif position[0] - x1 == 1 and position[1] == x2:
  46. #             position = [x1, x2]
  47. #             moves += 1
  48. #
  49. #     # Проверка за излизане от лабиринта
  50. #     if position[0] == 0 or position[0] == (len(maze) - 1) or position[1] == 0 or position[1] == (len(maze) - 1):
  51. #         return f'Kate got out in {moves + 1} moves'
  52. #     # Съобщение, че Kate не може да излезе от лабиринта
  53. #     return 'Kate cannot get out'
  54. #
  55. # # Вход от потребителя - брой редове в лабиринта
  56. # m_rows = int(input())
  57. # maze = []
  58. # moves = 0
  59. # free_space = True
  60. #
  61. # # Вход от потребителя - редове на лабиринта
  62. # for row in range(m_rows):
  63. #     maze.append(input())
  64. #
  65. # # Извикване на функциите и извеждане на резултата
  66. # position = find_position(maze)
  67. # next_free = next_free_spot(maze)
  68. # movement = find_path(position, next_free, maze)
  69. # print(movement)
  70.  
  71.  
  72. """ 2 """
  73.  
  74. # Функция за намиране на началната позиция на "k" в лабиринта
  75. def find_position(maze):
  76.     position = []
  77.     for row in range(len(maze)):
  78.         for el in maze[row]:
  79.             if el == 'k':
  80.                 position.append(row)
  81.                 position.append(maze[row].find('k'))
  82.                 return position
  83.  
  84. # Функция за намиране на свободните позиции (' ') в лабиринта
  85. def next_free_spot(maze):
  86.     free_spots = []
  87.  
  88.     for row in range(len(maze)):
  89.         for el in range(len(maze[row])):
  90.             tmp = []
  91.             if maze[row][el] == ' ':
  92.                 tmp.append(row)
  93.                 tmp.append(el)
  94.                 free_spots.append(tmp)
  95.  
  96.     return free_spots
  97.  
  98. # Функция за намиране на пътя в лабиринта и брой възможни изходи
  99. def find_path(position, next_free, maze):
  100.     total_moves = 0  # Общ брой на движенията
  101.     total_exits = 0  # Общ брой на възможните изходи
  102.     longest_paths = []  # Списък с най-дългите пътища
  103.  
  104.     while next_free:
  105.         x, y = next_free.pop(0)
  106.         moves = 0  # Брой на движенията за текущата позиция
  107.  
  108.         # Проверка за движение наляво
  109.         if position[0] == x and position[1] - y == 1:
  110.             moves += 1
  111.         # Проверка за движение надясно
  112.         elif position[0] == x and y - position[1] == 1:
  113.             moves += 1
  114.         # Проверка за движение надолу
  115.         elif x - position[0] == 1 and position[1] == y:
  116.             moves += 1
  117.         # Проверка за движение нагоре
  118.         elif position[0] - x == 1 and position[1] == y:
  119.             moves += 1
  120.  
  121.         # Обновяване на броя движения за текущата позиция
  122.         total_moves += moves
  123.  
  124.         # Проверка дали текущата позиция е изход от лабиринта
  125.         if position[0] == 0 or position[0] == (len(maze) - 1) or position[1] == 0 or position[1] == (len(maze[0]) - 1):
  126.             total_exits += 1
  127.  
  128.             # Запазване на текущия път
  129.             current_path = [(position[0], position[1])] + [(x, y) for x, y in next_free]
  130.             longest_paths.append(current_path)
  131.  
  132.     if total_exits > 0:
  133.         # Избор на най-дългия път
  134.         longest_path = max(longest_paths, key=len)
  135.         return f'"Kate got out in {total_moves +1} moves"'
  136.     else:
  137.         return 'Kate cannot get out'
  138.  
  139.  
  140. # Вход от потребителя - брой редове в лабиринта
  141. m_rows = int(input())
  142. maze = []
  143. moves = 0
  144. free_space = True
  145.  
  146. # Вход от потребителя - редове на лабиринта
  147. for row in range(m_rows):
  148.     maze.append(input())
  149.  
  150. # Извикване на функциите и извеждане на резултата
  151. position = find_position(maze)
  152. next_free = next_free_spot(maze)
  153. movement = find_path(position, next_free, maze)
  154. print(movement)
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement