Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def read_int_list_from_input():
- return [x for x in input()]
- def is_valid(row, col, matrix_rows, matrix_cols):
- right = 0 <= col + 1 < matrix_cols
- left = 0 <= col - 1 < matrix_cols
- up = 0 <= row - 1 < matrix_rows
- down = 0 <= row + 1 < matrix_rows
- return right, left, up, down
- def player_bunny_position(matrix_row, matrix_col, player_or_bunny):
- position = []
- for r in range(matrix_row):
- for c in range(matrix_col):
- if matrix[r][c] == player_or_bunny:
- position.append([r, c])
- return position
- def bunny_spreading(bunny_pos, matrix):
- matrix_rows = len(matrix)
- matrix_columns = len(matrix[0])
- bunny_row, bunny_col = bunny_pos
- right, left, up, down = is_valid(bunny_row, bunny_col, matrix_rows, matrix_columns)
- if right:
- matrix[bunny_row][bunny_col + 1] = "B"
- if left:
- matrix[bunny_row][bunny_col - 1] = "B"
- if up:
- matrix[bunny_row - 1][bunny_col] = "B"
- if down:
- matrix[bunny_row + 1][bunny_col] = "B"
- (rows_count, columns_count) = [int(x) for x in input().split()]
- matrix = [read_int_list_from_input() for _ in range(rows_count)]
- commands = input()
- complete = True
- player_pos = player_bunny_position(rows_count, columns_count, "P")
- player_row, player_col = player_pos[0][0], player_pos[0][1]
- for command in commands:
- bunnies = player_bunny_position(rows_count, columns_count, "B")
- for bunny in bunnies:
- bunny_spreading(bunny, matrix)
- print(f"{bunnies}||{command}")
- if command == "L":
- if (player_col - 1) < 0:
- if matrix[player_row][player_col] != "B":
- matrix[player_row][player_col] = "."
- break
- else:
- if matrix[player_row][player_col - 1] == "B":
- player_col -= 1
- complete = False
- break
- else:
- matrix[player_row][player_col - 1] = "P"
- matrix[player_row][player_col] = "."
- player_col -= 1
- elif command == "R":
- if (player_col + 1) > columns_count:
- if matrix[player_row][player_col] != "B":
- matrix[player_row][player_col] = "."
- break
- else:
- if matrix[player_row][player_col + 1] == "B":
- complete = False
- player_col += 1
- break
- else:
- matrix[player_row][player_col + 1] = "P"
- matrix[player_row][player_col] = "."
- player_col += 1
- elif command == "D":
- if (player_row + 1) > rows_count:
- if matrix[player_row][player_col] != "B":
- matrix[player_row][player_col] = "."
- break
- else:
- if matrix[player_row + 1][player_col] == "B":
- complete = False
- player_row += 1
- break
- else:
- matrix[player_row + 1][player_col] = "P"
- matrix[player_row][player_col] = "."
- player_row += 1
- elif command == "U":
- if (player_row - 1) < 0:
- if matrix[player_row][player_col] != "B":
- matrix[player_row][player_col] = "."
- break
- else:
- if matrix[player_row - 1][player_col] == "B":
- complete = False
- player_row -= 1
- break
- else:
- matrix[player_row - 1][player_col] = "P"
- matrix[player_row][player_col] = "."
- player_row -= 1
- for row in matrix:
- print("".join(row))
- if complete:
- print(f"won: {player_row} {player_col}")
- else:
- print(f"dead: {player_row} {player_col}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement