Advertisement
Kamend1

03. Kate's Way Out

Oct 6th, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. def find_all_paths(row, column, labyrinth, direction, path, all_paths):
  2. if row < 0 or column < 0 or row >= len(labyrinth) or column >= len(labyrinth[0]):
  3. return
  4. if labyrinth[row][column] == "#":
  5. return
  6. if labyrinth[row][column] == "v":
  7. return
  8.  
  9. path.append(direction)
  10.  
  11. if labyrinth[row][column] == "e":
  12. path_string = "".join(path)
  13. all_paths.append(path_string)
  14. else:
  15. labyrinth[row][column] = "v"
  16. find_all_paths(row - 1, column, labyrinth, "U", path, all_paths)
  17. find_all_paths(row + 1, column, labyrinth, "D", path, all_paths)
  18. find_all_paths(row, column + 1, labyrinth, "R", path, all_paths)
  19. find_all_paths(row, column - 1, labyrinth, "L", path, all_paths)
  20. labyrinth[row][column] = " "
  21.  
  22. path.pop()
  23. if len(all_paths) == 0:
  24. return None
  25.  
  26. return all_paths
  27.  
  28.  
  29. rows = int(input())
  30. labyrinth = []
  31.  
  32. for i in range(rows):
  33. labyrinth.append(list(input()))
  34.  
  35. columns = len(labyrinth[0])
  36. starting_row = 0
  37. starting_column = 0
  38. all_paths_kate = []
  39.  
  40. for row in range(rows):
  41. for column in range(columns):
  42. if labyrinth[row][column] == "k":
  43. starting_row = row
  44. starting_column = column
  45. if (row == 0 or row == rows - 1 or column == 0 or column == columns - 1) and labyrinth[row][column] == " ":
  46. labyrinth[row][column] = "e"
  47.  
  48. find_all_paths(starting_row, starting_column, labyrinth, "", [], all_paths_kate)
  49. max_len = 0
  50. for idx in range(len(all_paths_kate)):
  51. if len(all_paths_kate[idx]) > max_len:
  52. max_len = len(all_paths_kate[idx])
  53.  
  54. if find_all_paths(starting_row, starting_column, labyrinth, "", [], all_paths_kate) == None:
  55. print("Kate cannot get out")
  56. else:
  57. print(f"Kate got out in {max_len + 1} moves")
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement