from collections import defaultdict import numpy as np visited_cols = defaultdict(bool) visited_rows = defaultdict(bool) def find_rows(board): lines = [] for i in range(board.shape[0]): if visited_rows[i]: continue colors = defaultdict(int) colors['0'] = 0 for j in range(board.shape[1]): colors[board[i, j]] += 1 if len(colors) == 2: visited_rows[i] = True color_keys = list(colors.keys()) if color_keys[0] == '0': lines.append(('R', i, color_keys[1])) else: lines.append(('R', i, color_keys[0])) return lines def find_cols(board): lines = [] for i in range(board.shape[1]): if visited_cols[i]: continue colors = defaultdict(int) colors['0'] = 0 for j in range(board.shape[0]): colors[board[j, i]] += 1 if len(colors) == 2: visited_cols[i] = True color_keys = list(colors.keys()) if color_keys[0] == '0': lines.append(('K', i, color_keys[1])) else: lines.append(('K', i, color_keys[0])) return lines def remove_line(board, line): count = 0 if line[0] == 'R': for i in range(board.shape[1]): if board[line[1], i] != '0': count += 1 board[line[1], i] = '0' else: for i in range(board.shape[0]): if board[i, line[1]] != '0': count += 1 board[i, line[1]] = '0' return count n, m = map(int, input().split()) board = np.zeros((n, m), dtype='U1') for i in range(n): board[i, :] = [*input()] result = [] count = 0 rows = find_rows(board) cols = find_cols(board) if len(rows) > 0: lines = rows else: lines = cols while True: for line in lines: result.append(line) count += remove_line(board, line) if count == n*m: break if lines[0][0] == 'K': lines = find_rows(board) else: lines = find_cols(board) print(len(result)) for res in reversed(result): print(res[0], res[1]+1, res[2])
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 | from collections import defaultdict import numpy as np visited_cols = defaultdict(bool) visited_rows = defaultdict(bool) def find_rows(board): lines = [] for i in range(board.shape[0]): if visited_rows[i]: continue colors = defaultdict(int) colors['0'] = 0 for j in range(board.shape[1]): colors[board[i, j]] += 1 if len(colors) == 2: visited_rows[i] = True color_keys = list(colors.keys()) if color_keys[0] == '0': lines.append(('R', i, color_keys[1])) else: lines.append(('R', i, color_keys[0])) return lines def find_cols(board): lines = [] for i in range(board.shape[1]): if visited_cols[i]: continue colors = defaultdict(int) colors['0'] = 0 for j in range(board.shape[0]): colors[board[j, i]] += 1 if len(colors) == 2: visited_cols[i] = True color_keys = list(colors.keys()) if color_keys[0] == '0': lines.append(('K', i, color_keys[1])) else: lines.append(('K', i, color_keys[0])) return lines def remove_line(board, line): count = 0 if line[0] == 'R': for i in range(board.shape[1]): if board[line[1], i] != '0': count += 1 board[line[1], i] = '0' else: for i in range(board.shape[0]): if board[i, line[1]] != '0': count += 1 board[i, line[1]] = '0' return count n, m = map(int, input().split()) board = np.zeros((n, m), dtype='U1') for i in range(n): board[i, :] = [*input()] result = [] count = 0 rows = find_rows(board) cols = find_cols(board) if len(rows) > 0: lines = rows else: lines = cols while True: for line in lines: result.append(line) count += remove_line(board, line) if count == n*m: break if lines[0][0] == 'K': lines = find_rows(board) else: lines = find_cols(board) print(len(result)) for res in reversed(result): print(res[0], res[1]+1, res[2]) |