#include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; const int MAX_N = 2000; const int A = 26; int n, m; int cnt_col[MAX_N][A], cnt_row[MAX_N][A]; int diff_row[MAX_N], diff_col[MAX_N]; char grid[MAX_N][MAX_N]; bool vis_col[MAX_N], vis_row[MAX_N]; queue<pair<int, int>>q; void add_char(int row, int col, char c) { grid[row][col] = c; cnt_row[row][c - 'A']++; cnt_col[col][c - 'A']++; if(cnt_row[row][c - 'A'] == 1) diff_row[row]++; if(cnt_col[col][c - 'A'] == 1) diff_col[col]++; } void remove_char(int row, int col) { if(grid[row][col] == -1) return; int cid = grid[row][col] - 'A'; cnt_row[row][cid]--; cnt_col[col][cid]--; if(cnt_row[row][cid] == 0) diff_row[row]--; if(cnt_col[col][cid] == 0) diff_col[col]--; if(diff_row[row] == 1 && !vis_row[row]) { vis_row[row] = true; q.push(make_pair(0, row)); } if(diff_col[col] == 1 && !vis_col[col]) { vis_col[col] = true; q.push(make_pair(1, col)); } grid[row][col] = -1; } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for(int i = 0; i < n; i++) { string s; cin >> s; for(int j = 0; j < m; j++) { add_char(i, j, s[j]); } } for(int i = 0; i < n; i++) { if(diff_row[i] == 1) { q.push(make_pair(0, i)); } } for(int i = 0; i < m; i++) { if(diff_col[i] == 1) { q.push(make_pair(1, i)); } } struct op { char type; int id; char letter; op(char _type, int _id, char _letter) : type(_type), id(_id), letter(_letter) {} }; vector<op>ops; while(!q.empty()) { auto [t, num] = q.front(); q.pop(); if(t == 0) { // row for(int i = 0; i < A; i++) if(cnt_row[num][i] > 0) ops.push_back(op('R', num, i + 'A')); for(int j = 0; j < m; j++) remove_char(num, j); } else { // col for(int i = 0; i < A; i++) if(cnt_col[num][i] > 0) ops.push_back(op('K', num, i + 'A')); for(int i = 0; i < n; i++) remove_char(i, num); } } reverse(ops.begin(), ops.end()); cout << ops.size() << "\n"; for(auto [x, y, z] : ops) cout << x << " " << y + 1 << " " << z << "\n"; return 0; }
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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; const int MAX_N = 2000; const int A = 26; int n, m; int cnt_col[MAX_N][A], cnt_row[MAX_N][A]; int diff_row[MAX_N], diff_col[MAX_N]; char grid[MAX_N][MAX_N]; bool vis_col[MAX_N], vis_row[MAX_N]; queue<pair<int, int>>q; void add_char(int row, int col, char c) { grid[row][col] = c; cnt_row[row][c - 'A']++; cnt_col[col][c - 'A']++; if(cnt_row[row][c - 'A'] == 1) diff_row[row]++; if(cnt_col[col][c - 'A'] == 1) diff_col[col]++; } void remove_char(int row, int col) { if(grid[row][col] == -1) return; int cid = grid[row][col] - 'A'; cnt_row[row][cid]--; cnt_col[col][cid]--; if(cnt_row[row][cid] == 0) diff_row[row]--; if(cnt_col[col][cid] == 0) diff_col[col]--; if(diff_row[row] == 1 && !vis_row[row]) { vis_row[row] = true; q.push(make_pair(0, row)); } if(diff_col[col] == 1 && !vis_col[col]) { vis_col[col] = true; q.push(make_pair(1, col)); } grid[row][col] = -1; } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for(int i = 0; i < n; i++) { string s; cin >> s; for(int j = 0; j < m; j++) { add_char(i, j, s[j]); } } for(int i = 0; i < n; i++) { if(diff_row[i] == 1) { q.push(make_pair(0, i)); } } for(int i = 0; i < m; i++) { if(diff_col[i] == 1) { q.push(make_pair(1, i)); } } struct op { char type; int id; char letter; op(char _type, int _id, char _letter) : type(_type), id(_id), letter(_letter) {} }; vector<op>ops; while(!q.empty()) { auto [t, num] = q.front(); q.pop(); if(t == 0) { // row for(int i = 0; i < A; i++) if(cnt_row[num][i] > 0) ops.push_back(op('R', num, i + 'A')); for(int j = 0; j < m; j++) remove_char(num, j); } else { // col for(int i = 0; i < A; i++) if(cnt_col[num][i] > 0) ops.push_back(op('K', num, i + 'A')); for(int i = 0; i < n; i++) remove_char(i, num); } } reverse(ops.begin(), ops.end()); cout << ops.size() << "\n"; for(auto [x, y, z] : ops) cout << x << " " << y + 1 << " " << z << "\n"; return 0; } |