Advertisement
Davitkova

Kate

Jul 15th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. possible_directions = []
  2. steps_count = 0
  3. ways_out = list()
  4.  
  5.  
  6. def get_maze(rows_count):
  7.     maze_lst = []
  8.  
  9.     for input_row in range(rows_count):
  10.         input_row = list(input())
  11.         maze_lst.append(input_row)
  12.  
  13.     return maze_lst
  14.  
  15.  
  16. def get_kate_position(maze_lst):
  17.     k_position_x, k_position_y = 0, 0
  18.  
  19.     for row_index in range(len(maze_lst)):
  20.         if 'k' in maze_lst[row_index]:
  21.             k_position_y = row_index
  22.  
  23.             for position_index in range(len(maze_lst[row_index])):
  24.                 if maze_lst[row_index][position_index] == 'k':
  25.                     k_position_x = position_index
  26.                     return k_position_x, k_position_y
  27.  
  28.     return
  29.  
  30.  
  31. def kate_is_out(k_pos_x, k_pos_y, maze_lst):
  32.  
  33.     if k_pos_x == 0 or k_pos_x == (len(maze_lst[k_pos_y]) - 1) or k_pos_y == 0 or k_pos_y == (len(maze_lst) - 1):
  34.         return True
  35.  
  36.     return False
  37.  
  38.  
  39. def add_next_directions(k_position_x, k_position_y, maze_lst, forbidden_direction):
  40.     current_directions = list()
  41.  
  42.     if k_position_x > 0:
  43.         if maze_lst[k_position_y][k_position_x - 1] == " " and forbidden_direction != "left":
  44.             current_directions.append("left")
  45.  
  46.     if k_position_x < len(maze_lst[k_position_y]) - 1:
  47.         if maze_lst[k_position_y][k_position_x + 1] == " " and forbidden_direction != "right":
  48.             current_directions.append("right")
  49.  
  50.     if k_position_y > 0:
  51.         if maze_lst[k_position_y - 1][k_position_x] == " " and forbidden_direction != "up":
  52.             current_directions.append("up")
  53.  
  54.     if k_position_y < len(maze_lst) - 1:
  55.         if maze_lst[k_position_y + 1][k_position_x] == " " and forbidden_direction != "down":
  56.             current_directions.append("down")
  57.  
  58.     return current_directions
  59.  
  60.  
  61. def move_kate(move_direction, k_move_x, k_move_y):
  62.  
  63.     if move_direction == "left":
  64.         k_move_x -= 1
  65.  
  66.     elif move_direction == "right":
  67.  
  68.         k_move_x += 1
  69.     elif move_direction == "up":
  70.  
  71.         k_move_y -= 1
  72.     elif move_direction == "down":
  73.  
  74.         k_move_y += 1
  75.  
  76.     return k_move_x, k_move_y
  77.  
  78.  
  79. def ban_direction(current_direction):
  80.  
  81.     if current_direction == "left":
  82.         banned_direction = "right"
  83.  
  84.     if current_direction == "right":
  85.         banned_direction = "left"
  86.  
  87.     if current_direction == "up":
  88.         banned_direction = "down"
  89.  
  90.     if current_direction == "down":
  91.         banned_direction = "up"
  92.  
  93.     return banned_direction
  94.  
  95.  
  96. # Main starts here
  97. # Read the maze
  98. number_of_rows = int(input())
  99. maze = get_maze(number_of_rows)
  100.  
  101. # Get Kate position
  102. kate_position_x, kate_position_y = get_kate_position(maze)      # TODO handle None case
  103.  
  104. # Check is Kate can get out immediately
  105. if kate_is_out(kate_position_x, kate_position_y, maze):
  106.     ways_out.append(steps_count + 1)
  107.  
  108. # Get the directions where Kate can go initially
  109. possible_directions = add_next_directions(kate_position_x, kate_position_y, maze, None)
  110.  
  111. k_moved_position_x, k_moved_position_y = kate_position_x, kate_position_y
  112.  
  113. while possible_directions:
  114.  
  115.     steps_count += 1
  116.     # print(f"step: {steps_count}, directions: {possible_directions}")
  117.  
  118.     for direction in possible_directions:
  119.  
  120.         k_moved_position_x, k_moved_position_y = move_kate(direction, k_moved_position_x, k_moved_position_y)
  121.         # print(steps_count, direction, k_moved_position_x, k_moved_position_y)
  122.  
  123.         if kate_is_out(k_moved_position_x, k_moved_position_y, maze):
  124.             ways_out.append(steps_count + 1)
  125.             possible_directions = []    
  126.             break   # TODO change the directions lists somehow to work with multiple paths
  127.  
  128.         # set the opposite of the current direction to banned direction not to go back and loop
  129.         next_forbidden_direction = ban_direction(direction)
  130.  
  131.         possible_directions = add_next_directions(k_moved_position_x, k_moved_position_y, maze, next_forbidden_direction)
  132.         #print(possible_directions)
  133.  
  134.  
  135. if len(ways_out) == 0:
  136.     print("Kate cannot get out")
  137. else:
  138.     print(f"Kate got out in {max(ways_out)} moves")
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement