Advertisement
bl00dt3ars

03. Kate's Way Out

Jun 14th, 2021 (edited)
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. rows = int(input())
  2. matrix = [list(input()) for i in range(rows)]
  3. k_row = sum([r for r in range(rows) if "k" in matrix[r]])
  4. k_col = sum([c for c in range(len(matrix[k_row])) if "k" == matrix[k_row][c]])
  5.  
  6. r = k_row
  7. c = k_col
  8. visited_positions = [[k_row, k_col]]
  9. moves = 0
  10.  
  11. while True:
  12.     moves += 1
  13.     if matrix[r][c + 1] == " " and c + 1 < len(matrix[r]) and not [r, c + 1] in visited_positions:
  14.         matrix[r][c], matrix[r][c + 1] = matrix[r][c + 1], matrix[r][c]
  15.         c += 1
  16.         visited_positions.append([r, c])
  17.     elif matrix[r][c - 1] == " " and c - 1 >= 0 and not [r, c - 1] in visited_positions:
  18.         matrix[r][c], matrix[r][c - 1] = matrix[r][c - 1], matrix[r][c]
  19.         c -= 1
  20.         visited_positions.append([r, c])
  21.     elif matrix[r - 1][c] == " " and r - 1 >= 0 and not [r - 1, c] in visited_positions:
  22.         matrix[r][c], matrix[r - 1][c] = matrix[r - 1][c], matrix[r][c]
  23.         r -= 1
  24.         visited_positions.append([r, c])
  25.     elif matrix[r + 1][c] == " " and r + 1 < rows and not [r + 1, c] in visited_positions:
  26.         matrix[r][c], matrix[r + 1][c] = matrix[r + 1][c], matrix[r][c]
  27.         r += 1
  28.         visited_positions.append([r, c])
  29.     else:
  30.         moves = 0
  31.         matrix[r][c] = "#"
  32.         r = k_row
  33.         c = k_col
  34.         matrix[r][c] = "k"
  35.         visited_positions = [[k_row, k_col]]
  36.     if r == 0 or r == rows - 1 or c == 0 or c == len(matrix[0]) - 1:
  37.         moves += 1
  38.         print(f"Kate got out in {moves} moves")
  39.         break
  40.     elif matrix[r][c + 1] == "#" and matrix[r][c - 1] == "#" and matrix[r - 1][c] == "#" and matrix[r + 1][c] == "#":
  41.         print("Kate cannot get out")
  42.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement