n, m = [int(x) for x in input().split()] board = [] for i in range(0, n): board.append([x for x in input()]) k = int(input()) moves = input() def print_board(): for i in board: line = "".join(i) print(line) def verify_move(current, last_vertical, last_horizontal): if last_vertical == current or last_horizontal == current: return "" return current def reduce_duplicates(moves): last_vertical = "" last_horizontal = "" init_move = moves[0] reduced_moves = init_move if init_move == "G" or init_move == "D": last_vertical = init_move else: last_horizontal = init_move for i in range(1, len(moves)): current = moves[i] new_move = verify_move(current, last_vertical, last_horizontal) if len(new_move) > 0: if new_move == "G" or new_move == "D": last_vertical = new_move elif new_move == "L" or new_move == "P": last_horizontal = new_move reduced_moves = reduced_moves + new_move moves = reduced_moves return moves def reduce_groups(moves): i = 1 group_start = 0 groups = [] moves_copy = moves + "X" while i<len(moves_copy): if moves_copy[i] == "G" or moves_copy[i] == "D": if moves_copy[i-1] == "L" or moves_copy[i-1] == "P": groups.append(moves_copy[group_start:i]) group_start = i elif moves_copy[i] == "L" or moves_copy[i] == "P": if moves_copy[i-1] == "G" or moves_copy[i-1] == "D": groups.append(moves_copy[group_start:i]) group_start = i else: groups.append(moves_copy[group_start:i]) i += 1 moves = "".join([x[-1] for x in groups]) return moves def reduce_loop(moves): reductions_done = False while reductions_done == False: last_moves_len = len(moves) moves = reduce_groups(moves) print(moves) moves = reduce_duplicates(moves) print(moves) current_moves_len = len(moves) print(last_moves_len) print(current_moves_len) if last_moves_len == current_moves_len: reductions_done = True return moves def horizontal_move(move): #print("horizontal " + move) for i in range(0, n): sequence = [] for j in range(0, m): if board[i][j] != ".": sequence.append(board[i][j]) board[i][j] = "." shift = 0 if move == "P": shift += m-len(sequence) for s in range(0, len(sequence)): board[i][s+shift] = sequence[s] #print_board() def vertical_move(move): #print("vertical " + move) for j in range(0, m): sequence = [] for i in range(0, n): if board[i][j] != ".": sequence.append(board[i][j]) board[i][j] = "." shift = 0 if move == "D": shift += n-len(sequence) for s in range(0, len(sequence)): board[s+shift][j] = sequence[s] #print_board() def move_board(moves): for i in moves: if i == "G" or i == "D": vertical_move(i) if i == "L" or i == "P": horizontal_move(i) #print_board() #moves = reduce_loop(moves) #print(moves) move_board(moves) print_board()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | n, m = [int(x) for x in input().split()] board = [] for i in range(0, n): board.append([x for x in input()]) k = int(input()) moves = input() def print_board(): for i in board: line = "".join(i) print(line) def verify_move(current, last_vertical, last_horizontal): if last_vertical == current or last_horizontal == current: return "" return current def reduce_duplicates(moves): last_vertical = "" last_horizontal = "" init_move = moves[0] reduced_moves = init_move if init_move == "G" or init_move == "D": last_vertical = init_move else: last_horizontal = init_move for i in range(1, len(moves)): current = moves[i] new_move = verify_move(current, last_vertical, last_horizontal) if len(new_move) > 0: if new_move == "G" or new_move == "D": last_vertical = new_move elif new_move == "L" or new_move == "P": last_horizontal = new_move reduced_moves = reduced_moves + new_move moves = reduced_moves return moves def reduce_groups(moves): i = 1 group_start = 0 groups = [] moves_copy = moves + "X" while i<len(moves_copy): if moves_copy[i] == "G" or moves_copy[i] == "D": if moves_copy[i-1] == "L" or moves_copy[i-1] == "P": groups.append(moves_copy[group_start:i]) group_start = i elif moves_copy[i] == "L" or moves_copy[i] == "P": if moves_copy[i-1] == "G" or moves_copy[i-1] == "D": groups.append(moves_copy[group_start:i]) group_start = i else: groups.append(moves_copy[group_start:i]) i += 1 moves = "".join([x[-1] for x in groups]) return moves def reduce_loop(moves): reductions_done = False while reductions_done == False: last_moves_len = len(moves) moves = reduce_groups(moves) print(moves) moves = reduce_duplicates(moves) print(moves) current_moves_len = len(moves) print(last_moves_len) print(current_moves_len) if last_moves_len == current_moves_len: reductions_done = True return moves def horizontal_move(move): #print("horizontal " + move) for i in range(0, n): sequence = [] for j in range(0, m): if board[i][j] != ".": sequence.append(board[i][j]) board[i][j] = "." shift = 0 if move == "P": shift += m-len(sequence) for s in range(0, len(sequence)): board[i][s+shift] = sequence[s] #print_board() def vertical_move(move): #print("vertical " + move) for j in range(0, m): sequence = [] for i in range(0, n): if board[i][j] != ".": sequence.append(board[i][j]) board[i][j] = "." shift = 0 if move == "D": shift += n-len(sequence) for s in range(0, len(sequence)): board[s+shift][j] = sequence[s] #print_board() def move_board(moves): for i in moves: if i == "G" or i == "D": vertical_move(i) if i == "L" or i == "P": horizontal_move(i) #print_board() #moves = reduce_loop(moves) #print(moves) move_board(moves) print_board() |