Advertisement
DiYane

Kate's way out

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