Advertisement
Guest User

Untitled

a guest
Sep 21st, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. def read_int_list_from_input():
  2. return [x for x in input()]
  3.  
  4.  
  5. def is_valid(row, col, matrix_rows, matrix_cols):
  6. right = 0 <= col + 1 < matrix_cols
  7. left = 0 <= col - 1 < matrix_cols
  8. up = 0 <= row - 1 < matrix_rows
  9. down = 0 <= row + 1 < matrix_rows
  10. return right, left, up, down
  11.  
  12.  
  13. def player_bunny_position(matrix_row, matrix_col, player_or_bunny):
  14. position = []
  15. for r in range(matrix_row):
  16. for c in range(matrix_col):
  17. if matrix[r][c] == player_or_bunny:
  18. position.append([r, c])
  19. return position
  20.  
  21.  
  22. def bunny_spreading(bunny_pos, matrix):
  23. matrix_rows = len(matrix)
  24. matrix_columns = len(matrix[0])
  25. bunny_row, bunny_col = bunny_pos
  26. right, left, up, down = is_valid(bunny_row, bunny_col, matrix_rows, matrix_columns)
  27. if right:
  28. matrix[bunny_row][bunny_col + 1] = "B"
  29. if left:
  30. matrix[bunny_row][bunny_col - 1] = "B"
  31. if up:
  32. matrix[bunny_row - 1][bunny_col] = "B"
  33. if down:
  34. matrix[bunny_row + 1][bunny_col] = "B"
  35.  
  36.  
  37. (rows_count, columns_count) = [int(x) for x in input().split()]
  38. matrix = [read_int_list_from_input() for _ in range(rows_count)]
  39. commands = input()
  40.  
  41. complete = True
  42.  
  43. player_pos = player_bunny_position(rows_count, columns_count, "P")
  44. player_row, player_col = player_pos[0][0], player_pos[0][1]
  45.  
  46. for command in commands:
  47. bunnies = player_bunny_position(rows_count, columns_count, "B")
  48. for bunny in bunnies:
  49. bunny_spreading(bunny, matrix)
  50.  
  51. print(f"{bunnies}||{command}")
  52.  
  53. if command == "L":
  54. if (player_col - 1) < 0:
  55. if matrix[player_row][player_col] != "B":
  56. matrix[player_row][player_col] = "."
  57. break
  58. else:
  59. if matrix[player_row][player_col - 1] == "B":
  60. player_col -= 1
  61. complete = False
  62. break
  63. else:
  64. matrix[player_row][player_col - 1] = "P"
  65. matrix[player_row][player_col] = "."
  66. player_col -= 1
  67. elif command == "R":
  68. if (player_col + 1) > columns_count:
  69. if matrix[player_row][player_col] != "B":
  70. matrix[player_row][player_col] = "."
  71. break
  72. else:
  73. if matrix[player_row][player_col + 1] == "B":
  74. complete = False
  75. player_col += 1
  76. break
  77. else:
  78. matrix[player_row][player_col + 1] = "P"
  79. matrix[player_row][player_col] = "."
  80. player_col += 1
  81. elif command == "D":
  82. if (player_row + 1) > rows_count:
  83. if matrix[player_row][player_col] != "B":
  84. matrix[player_row][player_col] = "."
  85. break
  86. else:
  87. if matrix[player_row + 1][player_col] == "B":
  88. complete = False
  89. player_row += 1
  90. break
  91. else:
  92. matrix[player_row + 1][player_col] = "P"
  93. matrix[player_row][player_col] = "."
  94. player_row += 1
  95. elif command == "U":
  96. if (player_row - 1) < 0:
  97. if matrix[player_row][player_col] != "B":
  98. matrix[player_row][player_col] = "."
  99. break
  100. else:
  101. if matrix[player_row - 1][player_col] == "B":
  102. complete = False
  103. player_row -= 1
  104. break
  105. else:
  106. matrix[player_row - 1][player_col] = "P"
  107. matrix[player_row][player_col] = "."
  108. player_row -= 1
  109.  
  110. for row in matrix:
  111. print("".join(row))
  112.  
  113. if complete:
  114. print(f"won: {player_row} {player_col}")
  115. else:
  116. print(f"dead: {player_row} {player_col}")
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement