#include <bits/stdc++.h> using namespace std; const int MAXN = 2005; char s[MAXN][MAXN]; int lic_w[MAXN][100]; int lic_k[MAXN][100]; vector<pair<int, int>> v; vector<pair<int, pair<int, char>>> res; int main() { int n, m; scanf("%d%d", &n, &m); for (int i=0; i<n; i++) { scanf("%s", s[i]); } for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { if (lic_w[i][s[i][j]]++ == 0) lic_w[i][0]++; if (lic_k[j][s[i][j]]++ == 0) lic_k[j][0]++; } } for (int i=0; i<n; i++) { if (lic_w[i][0] == 1) v.push_back({0, i}); } for (int j=0; j<m; j++) { if (lic_k[j][0] == 1) v.push_back({1, j}); } while (!v.empty()) { auto p = v.back(); v.pop_back(); char color = 'A'; if (p.first == 0) { // row int i = p.second; for (int j=0; j<m; j++) { if (s[i][j] == -1) continue; color = s[i][j]; if (--lic_k[j][s[i][j]] == 0) if (--lic_k[j][0] == 1) v.push_back({1, j}); s[i][j] = -1; } res.push_back({0, {i, color}}); } else { // column int j = p.second; for (int i=0; i<n; i++) { if (s[i][j] == -1) continue; color = s[i][j]; if (--lic_w[i][s[i][j]] == 0) if (--lic_w[i][0] == 1) v.push_back({0, i}); s[i][j] = -1; } res.push_back({1, {j, color}}); } } printf("%d\n", (int)res.size()); for (int i=res.size()-1; i >= 0; i--) { printf("%c %d %c\n", res[i].first == 0 ? 'R' : 'K', res[i].second.first+1, res[i].second.second); } 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 | #include <bits/stdc++.h> using namespace std; const int MAXN = 2005; char s[MAXN][MAXN]; int lic_w[MAXN][100]; int lic_k[MAXN][100]; vector<pair<int, int>> v; vector<pair<int, pair<int, char>>> res; int main() { int n, m; scanf("%d%d", &n, &m); for (int i=0; i<n; i++) { scanf("%s", s[i]); } for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { if (lic_w[i][s[i][j]]++ == 0) lic_w[i][0]++; if (lic_k[j][s[i][j]]++ == 0) lic_k[j][0]++; } } for (int i=0; i<n; i++) { if (lic_w[i][0] == 1) v.push_back({0, i}); } for (int j=0; j<m; j++) { if (lic_k[j][0] == 1) v.push_back({1, j}); } while (!v.empty()) { auto p = v.back(); v.pop_back(); char color = 'A'; if (p.first == 0) { // row int i = p.second; for (int j=0; j<m; j++) { if (s[i][j] == -1) continue; color = s[i][j]; if (--lic_k[j][s[i][j]] == 0) if (--lic_k[j][0] == 1) v.push_back({1, j}); s[i][j] = -1; } res.push_back({0, {i, color}}); } else { // column int j = p.second; for (int i=0; i<n; i++) { if (s[i][j] == -1) continue; color = s[i][j]; if (--lic_w[i][s[i][j]] == 0) if (--lic_w[i][0] == 1) v.push_back({0, i}); s[i][j] = -1; } res.push_back({1, {j, color}}); } } printf("%d\n", (int)res.size()); for (int i=res.size()-1; i >= 0; i--) { printf("%c %d %c\n", res[i].first == 0 ? 'R' : 'K', res[i].second.first+1, res[i].second.second); } return 0; } |