#include <bits/stdc++.h> using namespace std; #define ll long long #define maxn 2010 #define maxa 30 #define result_t pair<bool, pair<int,char>> char board[maxn][maxn]; bool removed[maxn][maxn]; queue<result_t> next_to_do; vector<result_t> output; int count_column[maxn][maxa], count_row[maxn][maxa], different_column[maxn], different_row[maxn]; void add_pixel(int row, int column, char c) { count_column[column][c-'A']++; count_row[row][c-'A']++; if (count_column[column][c-'A'] == 1) { different_column[column]++; } if (count_row[row][c-'A'] == 1) { different_row[row]++; } } char find_color(bool row, int idx) { for(int i = 0;; ++i) { if (row && count_row[idx][i] != 0) { return 'A' + i; } else if (!row && count_column[idx][i] != 0) { return 'A' + i; } } } void conditional_push(int val, bool row, int idx) { if (val == 1) { next_to_do.push({row, {idx, find_color(row, idx)}}); } } void remove_pixel(int row, int column) { if (removed[row][column]) { return; } else { removed[row][column] = true; } int c = board[row][column] - 'A'; count_column[column][c]--; count_row[row][c]--; if (count_column[column][c] == 0) { different_column[column]--; conditional_push(different_column[column], false, column); } if (count_row[row][c] == 0) { different_row[row]--; conditional_push(different_row[row], true, row); } } void initialize(int n, int m) { for (int i = 0; i < n; ++i) { conditional_push(different_row[i], true, i); } for (int i = 0; i < m; ++i) { conditional_push(different_column[i], false, i); } } void output_print() { cout << (int)output.size() << endl; for (int i = (int)output.size() - 1; i >= 0; --i) { auto el = output[i]; char op_type = el.first ? 'R' : 'K'; cout << op_type << " " << el.second.first + 1 << " " << el.second.second << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m; cin >> n >> m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> board[i][j]; add_pixel(i, j, board[i][j]); } } initialize(n, m); while(!next_to_do.empty()) { auto el = next_to_do.front(); next_to_do.pop(); output.push_back(el); if (el.first) { //is row for (int j = 0; j < m; ++j) { remove_pixel(el.second.first, j); } } else { for (int j = 0; j < n; ++j) { remove_pixel(j, el.second.first); } } } output_print(); }
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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define maxn 2010 #define maxa 30 #define result_t pair<bool, pair<int,char>> char board[maxn][maxn]; bool removed[maxn][maxn]; queue<result_t> next_to_do; vector<result_t> output; int count_column[maxn][maxa], count_row[maxn][maxa], different_column[maxn], different_row[maxn]; void add_pixel(int row, int column, char c) { count_column[column][c-'A']++; count_row[row][c-'A']++; if (count_column[column][c-'A'] == 1) { different_column[column]++; } if (count_row[row][c-'A'] == 1) { different_row[row]++; } } char find_color(bool row, int idx) { for(int i = 0;; ++i) { if (row && count_row[idx][i] != 0) { return 'A' + i; } else if (!row && count_column[idx][i] != 0) { return 'A' + i; } } } void conditional_push(int val, bool row, int idx) { if (val == 1) { next_to_do.push({row, {idx, find_color(row, idx)}}); } } void remove_pixel(int row, int column) { if (removed[row][column]) { return; } else { removed[row][column] = true; } int c = board[row][column] - 'A'; count_column[column][c]--; count_row[row][c]--; if (count_column[column][c] == 0) { different_column[column]--; conditional_push(different_column[column], false, column); } if (count_row[row][c] == 0) { different_row[row]--; conditional_push(different_row[row], true, row); } } void initialize(int n, int m) { for (int i = 0; i < n; ++i) { conditional_push(different_row[i], true, i); } for (int i = 0; i < m; ++i) { conditional_push(different_column[i], false, i); } } void output_print() { cout << (int)output.size() << endl; for (int i = (int)output.size() - 1; i >= 0; --i) { auto el = output[i]; char op_type = el.first ? 'R' : 'K'; cout << op_type << " " << el.second.first + 1 << " " << el.second.second << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m; cin >> n >> m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> board[i][j]; add_pixel(i, j, board[i][j]); } } initialize(n, m); while(!next_to_do.empty()) { auto el = next_to_do.front(); next_to_do.pop(); output.push_back(el); if (el.first) { //is row for (int j = 0; j < m; ++j) { remove_pixel(el.second.first, j); } } else { for (int j = 0; j < n; ++j) { remove_pixel(j, el.second.first); } } } output_print(); } |