Guest User

5.Kate's Way Out

a guest
Mar 22nd, 2021
1,368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. def find_Kate(mx):
  2.     for i in range(len(mx)):
  3.         for j in range(len(mx[i])):
  4.             if mx[i][j] == 'k':
  5.                 return i, j
  6.  
  7.  
  8. def move_Kate(mx, directions, moves=[], count=0):
  9.     while True:
  10.         position = find_Kate(mx)
  11.         if position is None:
  12.             if not moves:
  13.                 return 'Kate cannot get out'
  14.             else:
  15.                 return min(moves)
  16.         else:
  17.             r, c = position
  18.         mx[r][c] = '$'
  19.         for direction in directions:
  20.             if r + directions[direction][0] < len(mx) and 0 <= c + directions[direction][1] < len(mx[r]):
  21.                 if mx[r + directions[direction][0]][c + directions[direction][1]] == ' ':
  22.                     mx[r + directions[direction][0]][c + directions[direction][1]] = 'k'
  23.                     move_Kate(mx, directions, moves, count + 1)
  24.             else:
  25.                 count += 1
  26.                 moves.append(count)
  27.  
  28.  
  29. maze = [list(input()) for _ in range(int(input()))]
  30. movement = {
  31.     'U': [-1, 0],
  32.     'D': [1, 0],
  33.     'R': [0, 1],
  34.     'L': [0, -1],
  35. }
  36. res = move_Kate(maze, movement)
  37. if str(res).isdigit():
  38.     print(f'Kate got out in {res} moves')
  39. else:
  40.     print(res)
  41.    
Advertisement
Add Comment
Please, Sign In to add comment