from collections import Counter def get_input(): n, m = [int(x) for x in input().split(' ')] matrix = [list(input()) for _ in range(n)] return n, m, matrix def get_row_counter(matrix): return [Counter(row) for row in matrix] def get_col_counter(matrix): return [Counter([row[i] for row in matrix]) for i in range(len(matrix[0]))] n, m, matrix = get_input() row_counter = get_row_counter(matrix) col_counter = get_col_counter(matrix) single_color_rows = {i for i, c in enumerate(row_counter) if len(c) == 1} single_color_cols = {i for i, c in enumerate(col_counter) if len(c) == 1} colored_squares = n * m moves = [] done_rows = set() done_cols = set() while colored_squares > 0: for row in single_color_rows: if row not in done_rows and row_counter[row]: row_color = list(row_counter[row].keys())[0] moves.append(f'R {row + 1} {row_color}') del row_counter[row][row_color] for col_index in range(m): color = matrix[row][col_index] if color is not None: colored_squares -= 1 matrix[row][col_index] = None col_counter[col_index][color] -= 1 if col_counter[col_index][color] == 0: del col_counter[col_index][color] if len(col_counter[col_index]) == 1: single_color_cols.add(col_index) single_color_rows.clear() for col in single_color_cols: if col not in done_cols and col_counter[col]: col_color = list(col_counter[col].keys())[0] moves.append(f'K {col + 1} {col_color}') del col_counter[col][col_color] for row_index in range(n): color = matrix[row_index][col] if color is not None: colored_squares -= 1 matrix[row_index][col] = None row_counter[row_index][color] -= 1 if row_counter[row_index][color] == 0: del row_counter[row_index][color] if len(row_counter[row_index]) == 1: single_color_rows.add(row_index) single_color_cols.clear() print(len(moves)) for move in moves[::-1]: print(move)
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 | from collections import Counter def get_input(): n, m = [int(x) for x in input().split(' ')] matrix = [list(input()) for _ in range(n)] return n, m, matrix def get_row_counter(matrix): return [Counter(row) for row in matrix] def get_col_counter(matrix): return [Counter([row[i] for row in matrix]) for i in range(len(matrix[0]))] n, m, matrix = get_input() row_counter = get_row_counter(matrix) col_counter = get_col_counter(matrix) single_color_rows = {i for i, c in enumerate(row_counter) if len(c) == 1} single_color_cols = {i for i, c in enumerate(col_counter) if len(c) == 1} colored_squares = n * m moves = [] done_rows = set() done_cols = set() while colored_squares > 0: for row in single_color_rows: if row not in done_rows and row_counter[row]: row_color = list(row_counter[row].keys())[0] moves.append(f'R {row + 1} {row_color}') del row_counter[row][row_color] for col_index in range(m): color = matrix[row][col_index] if color is not None: colored_squares -= 1 matrix[row][col_index] = None col_counter[col_index][color] -= 1 if col_counter[col_index][color] == 0: del col_counter[col_index][color] if len(col_counter[col_index]) == 1: single_color_cols.add(col_index) single_color_rows.clear() for col in single_color_cols: if col not in done_cols and col_counter[col]: col_color = list(col_counter[col].keys())[0] moves.append(f'K {col + 1} {col_color}') del col_counter[col][col_color] for row_index in range(n): color = matrix[row_index][col] if color is not None: colored_squares -= 1 matrix[row_index][col] = None row_counter[row_index][color] -= 1 if row_counter[row_index][color] == 0: del row_counter[row_index][color] if len(row_counter[row_index]) == 1: single_color_rows.add(row_index) single_color_cols.clear() print(len(moves)) for move in moves[::-1]: print(move) |