go6odn28

kate's_way_out

Feb 24th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. def correct_lab_bounds(row, col):
  2.     if row < 0 or col < 0 or row >= len(lab_list) or col >= len(lab_list[0]):
  3.         return True
  4.  
  5.  
  6. def check_wall(row, col):
  7.     if lab_list[row][col] in "#v":
  8.         return True
  9.  
  10.  
  11. def find_exit(row, col):
  12.     if row == 0 or row == len(lab_list) - 1 or col == 0 or col == len(lab_list[0]):
  13.         return True
  14.  
  15.  
  16. def find_starting_point():
  17.     for pos_row, row in enumerate(lab_list):
  18.         for pos_col, col in enumerate(row):
  19.             if col == "k":
  20.                 return pos_row, pos_col
  21.  
  22.  
  23. def find_the_lab_path(row, col, lab):
  24.     if correct_lab_bounds(row, col) or check_wall(row, col):
  25.         return
  26.  
  27.     steps.append(1)
  28.  
  29.     if find_exit(row, col):
  30.         max_len_path.append(sum(steps))
  31.  
  32.     lab[row][col] = "v"
  33.     find_the_lab_path(row, col + 1, lab)  # check right
  34.     find_the_lab_path(row, col - 1, lab)  # check left
  35.     find_the_lab_path(row + 1, col, lab)  # check up
  36.     find_the_lab_path(row - 1, col, lab)  # check down
  37.     lab[row][col] = " "
  38.  
  39.     steps.pop()
  40.  
  41.  
  42. rows = int(input())
  43. lab_list = []
  44. steps = []
  45. max_len_path = []
  46. for curr_lab in range(rows):
  47.     lab_list.append(list(input()))
  48. cols = len(lab_list[0])
  49. start_row, start_col = find_starting_point()
  50.  
  51. find_the_lab_path(start_row, start_col, lab_list)
  52.  
  53. if max_len_path:
  54.     print(f"Kate got out in {max(max_len_path)} moves")
  55. else:
  56.     print("Kate cannot get out")
  57.  
Add Comment
Please, Sign In to add comment