Advertisement
Nenogzar

3. Kate's Way Out

Feb 12th, 2024
1,058
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 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.  
Advertisement
Comments
  • Nenogzar
    111 days
    # text 0.76 KB | 0 0
    1. Kate is stuck in a maze. You should help her to find her way out.
    2. On the first line, you will be given how many rows there are in the maze. On the following n lines, you will be given the maze itself. Here is a legend for the maze:
    3. • "#" - means a wall; Kate cannot go through there
    4. • " " - means empty space; Kate can go through there
    5. • "k" - the initial position of Kate; start looking for a way out from there
    6. There are two options: Kate either gets out or not:
    7. • If Kate can get out, print the following:
    8. "Kate got out in {number_of_moves} moves".
    9. Note: If there are two or more ways out, she always chooses the longest one.
    10. • Otherwise, print: "Kate cannot get out".
    11.  
    12. Input
    13. 4
    14. ######
    15. ## k#
    16. ## ###
    17. ## ###
    18.  
    19. Output:
    20. Kate got out in 5 moves
    21.  
Add Comment
Please, Sign In to add comment
Advertisement