Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def next_move(playerrow, playercol, command_):
- if command_ == "U":
- if playerrow - 1 >= 0:
- playerrow -= 1
- elif command_ == "D":
- if playerrow + 1 < n:
- playerrow += 1
- elif command_ == "R":
- if playercol + 1 < m:
- playercol += 1
- elif command_ == "L":
- if playercol - 1 >= 0:
- playercol -= 1
- return playerrow, playercol
- def new_bunnies(bunny_row_, bunny_col_):
- new_bunnies_ = []
- if bunny_row_ - 1 >= 0:
- new_bunnies_.append((bunny_row_ - 1, bunny_col_))
- if bunny_col_ - 1 >= 0:
- new_bunnies_.append((bunny_row_, bunny_col - 1))
- if bunny_col_ + 1 < m:
- new_bunnies_.append((bunny_row_, bunny_col_ + 1))
- if bunny_row_ + 1 < n:
- new_bunnies_.append((bunny_row_ + 1, bunny_col_))
- return new_bunnies_
- n, m = [int(n) for n in input().split()]
- is_won = False
- is_dead = False
- bunnies_coordinates = set()
- matrix = []
- player_row, player_col = None, None
- for row in range(n):
- matrix.append(list(input()))
- for col in range(m):
- if matrix[row][col] == "B":
- bunnies_coordinates.add((row, col))
- elif matrix[row][col] == "P":
- player_row, player_col = row, col
- commands = list(input())
- while not is_won and not is_dead:
- for command in commands:
- new_current_bunnies = []
- next_row, next_col = next_move(player_row, player_col, command)
- if (next_row, next_col) == (player_row, player_col):
- is_won = True
- matrix[next_row][next_col] = "."
- else:
- matrix[player_row][player_col] = "."
- player_row, player_col = next_row, next_col
- if matrix[player_row][player_col] == "B":
- is_dead = True
- for bunny_row, bunny_col in bunnies_coordinates:
- new_current_bunnies = set(new_bunnies(bunny_row, bunny_col))
- for new_b_r, new_b_c in new_current_bunnies:
- if (new_b_r, new_b_c) == (player_row, player_col):
- is_dead = True
- matrix[new_b_r][new_b_c] = "B"
- bunnies_coordinates = bunnies_coordinates.union(new_current_bunnies)
- if is_won or is_dead:
- break
- for row in range(n):
- print(*matrix[row], sep='')
- if is_won:
- print(f"won: {player_row} {player_col}")
- elif is_dead:
- print(f"dead: {player_row} {player_col}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement