Advertisement
viligen

radioactive_mutant_bunnies

Jan 23rd, 2022
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. def next_move(playerrow, playercol, command_):
  2.     if command_ == "U":
  3.         if playerrow - 1 >= 0:
  4.             playerrow -= 1
  5.     elif command_ == "D":
  6.         if playerrow + 1 < n:
  7.             playerrow += 1
  8.     elif command_ == "R":
  9.         if playercol + 1 < m:
  10.             playercol += 1
  11.     elif command_ == "L":
  12.         if playercol - 1 >= 0:
  13.             playercol -= 1
  14.     return playerrow, playercol
  15.  
  16.  
  17. def new_bunnies(bunny_row_, bunny_col_):
  18.     new_bunnies_ = []
  19.     if bunny_row_ - 1 >= 0:
  20.         new_bunnies_.append((bunny_row_ - 1, bunny_col_))
  21.     if bunny_col_ - 1 >= 0:
  22.         new_bunnies_.append((bunny_row_, bunny_col - 1))
  23.     if bunny_col_ + 1 < m:
  24.         new_bunnies_.append((bunny_row_, bunny_col_ + 1))
  25.     if bunny_row_ + 1 < n:
  26.         new_bunnies_.append((bunny_row_ + 1, bunny_col_))
  27.     return new_bunnies_
  28.  
  29.  
  30. n, m = [int(n) for n in input().split()]
  31.  
  32. is_won = False
  33. is_dead = False
  34.  
  35. bunnies_coordinates = set()
  36. matrix = []
  37. player_row, player_col = None, None
  38.  
  39. for row in range(n):
  40.     matrix.append(list(input()))
  41.     for col in range(m):
  42.         if matrix[row][col] == "B":
  43.             bunnies_coordinates.add((row, col))
  44.         elif matrix[row][col] == "P":
  45.             player_row, player_col = row, col
  46.  
  47. commands = list(input())
  48.  
  49. while not is_won and not is_dead:
  50.     for command in commands:
  51.         new_current_bunnies = []
  52.         next_row, next_col = next_move(player_row, player_col, command)
  53.         if (next_row, next_col) == (player_row, player_col):
  54.             is_won = True
  55.             matrix[next_row][next_col] = "."
  56.  
  57.         else:
  58.             matrix[player_row][player_col] = "."
  59.             player_row, player_col = next_row, next_col
  60.             if matrix[player_row][player_col] == "B":
  61.                 is_dead = True
  62.  
  63.         for bunny_row, bunny_col in bunnies_coordinates:
  64.             new_current_bunnies = set(new_bunnies(bunny_row, bunny_col))
  65.             for new_b_r, new_b_c in new_current_bunnies:
  66.                 if (new_b_r, new_b_c) == (player_row, player_col):
  67.                     is_dead = True
  68.  
  69.                 matrix[new_b_r][new_b_c] = "B"
  70.             bunnies_coordinates = bunnies_coordinates.union(new_current_bunnies)
  71.         if is_won or is_dead:
  72.             break
  73.  
  74. for row in range(n):
  75.     print(*matrix[row], sep='')
  76.  
  77. if is_won:
  78.     print(f"won: {player_row} {player_col}")
  79. elif is_dead:
  80.     print(f"dead: {player_row} {player_col}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement