Guest User

Untitled

a guest
Feb 11th, 2021
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. def find_player(matrix):
  2. for y, row in enumerate(matrix):
  3. for x, value in enumerate(row):
  4. if value == "P":
  5. return x, y
  6.  
  7.  
  8. def find_bunnies(matrix):
  9. bunnies = []
  10. for y, row in enumerate(matrix):
  11. for x, value in enumerate(row):
  12. if value == "B":
  13. bunnies.append((x, y))
  14. return bunnies
  15.  
  16.  
  17. n, m = [int(x) for x in input().split(' ')]
  18. lair = [list(input()) for _ in range(n)]
  19. directions = input()
  20. player_x, player_y = find_player(lair)
  21. bunny_multiplications_directions = [
  22. (0, 1),
  23. (0, -1),
  24. (1, 0),
  25. (-1, 0),
  26. ]
  27.  
  28. last_player_x, last_player_y = None, None
  29.  
  30. won = False
  31. lost = False
  32.  
  33. for direction in directions:
  34. lair[player_y][player_x] = '.'
  35. last_player_x = player_x
  36. last_player_y = player_y
  37.  
  38. if direction == 'U':
  39. player_y -= 1
  40. elif direction == 'D':
  41. player_y += 1
  42. elif direction == 'L':
  43. player_x -= 1
  44. elif direction == 'R':
  45. player_x += 1
  46.  
  47. if player_x < 0 or player_x >= m or player_y >= n or player_y < 0:
  48. won = True
  49. else:
  50. at_position = lair[player_y][player_x]
  51. if at_position == 'B':
  52. lost = True
  53. last_player_x = player_x
  54. last_player_y = player_y
  55. else:
  56. lair[player_y][player_x] = 'P'
  57.  
  58. for bunny_x, bunny_y in find_bunnies(lair):
  59. for delta_x, delta_y in bunny_multiplications_directions:
  60. new_bunny_x = bunny_x + delta_x
  61. new_bunny_y = bunny_y + delta_y
  62. if 0 <= new_bunny_x < m and 0 <= new_bunny_y < n:
  63. at_position = lair[new_bunny_y][new_bunny_x]
  64. lair[new_bunny_y][new_bunny_x] = 'B'
  65. if at_position == 'P':
  66. lost = True
  67.  
  68. if won:
  69. break
  70. if lost:
  71. break
  72. for row in lair:
  73. print(''.join(row))
  74.  
  75. if won:
  76. print(f'won: {last_player_y} {last_player_x}')
  77. elif lost:
  78. print(f'dead: {last_player_y} {last_player_x}')
Advertisement
Add Comment
Please, Sign In to add comment