#include <bits/stdc++.h> using namespace std; const int maxn = 2005; map<char, int> row[maxn], column[maxn]; char a[maxn][maxn]; bool used_row[maxn], used_column[maxn]; struct Node { char c; int x; char letter; }; void add(vector<Node>& ans, char c, int i, char x) { Node n; n.c = c; n.x = i; n.letter = x; ans.push_back(n); } int main() { //freopen("input.txt", "r", stdin); int n, m; cin >> n >> m; for (int i = 1; i <= n; ++i) used_row[i] = true; for (int j = 1; j <= m; ++j) used_column[j] = true; for (int i = 1; i <= n; ++i) { string s; cin >> s; for (int j = 1; j <= m; ++j) { a[i][j] = s[j - 1]; row[i][a[i][j]]++; column[j][a[i][j]]++; } } vector<pair<int, int> > cur; vector<Node> ans; for (int i = 1; i <= n; ++i) { if (row[i].size() == 1) { cur.push_back({i, 0}); used_row[i] = false; pair<char, int> t = *(row[i].begin()); add(ans, 'R', i, t.first); } } for (int j = 1; j <= m; ++j) { if (column[j].size() == 1) { cur.push_back({j, 1}); used_column[j] = false; pair<char, int> t = *(column[j].begin()); add(ans, 'K', j, t.first); } } int head = 0, tail = cur.size(); while (head < tail) { pair<int, int> x = cur[head]; if (x.second == 0) { int i = x.first; used_row[i] = false; for (int j = 1; j <= m; ++j) { if (a[i][j] == '.') continue; --column[j][a[i][j]]; if (column[j][a[i][j]] == 0) column[j].erase(a[i][j]); if (column[j].size() == 1 && used_column[j]) { used_column[j] = false; cur.push_back({j, 1}); pair<char, int> t = *(column[j].begin()); add(ans, 'K', j, t.first); ++tail; } a[i][j] = '.'; } } else { int j = x.first; used_column[j] = false; for (int i = 1; i <= n; ++i) { if (a[i][j] == '.') continue; --row[i][a[i][j]]; if (row[i][a[i][j]] == 0) row[i].erase(a[i][j]); if (row[i].size() == 1 && used_row[i]) { used_row[i] = false; cur.push_back({i, 0}); pair<char, int> t = *(row[i].begin()); add(ans, 'R', i, t.first); ++tail; } a[i][j] = '.'; } } ++head; } reverse(ans.begin(), ans.end()); cout << ans.size() << endl; for (const auto& x : ans) { cout << x.c << " " << x.x << " " << x.letter << endl; } 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 | #include <bits/stdc++.h> using namespace std; const int maxn = 2005; map<char, int> row[maxn], column[maxn]; char a[maxn][maxn]; bool used_row[maxn], used_column[maxn]; struct Node { char c; int x; char letter; }; void add(vector<Node>& ans, char c, int i, char x) { Node n; n.c = c; n.x = i; n.letter = x; ans.push_back(n); } int main() { //freopen("input.txt", "r", stdin); int n, m; cin >> n >> m; for (int i = 1; i <= n; ++i) used_row[i] = true; for (int j = 1; j <= m; ++j) used_column[j] = true; for (int i = 1; i <= n; ++i) { string s; cin >> s; for (int j = 1; j <= m; ++j) { a[i][j] = s[j - 1]; row[i][a[i][j]]++; column[j][a[i][j]]++; } } vector<pair<int, int> > cur; vector<Node> ans; for (int i = 1; i <= n; ++i) { if (row[i].size() == 1) { cur.push_back({i, 0}); used_row[i] = false; pair<char, int> t = *(row[i].begin()); add(ans, 'R', i, t.first); } } for (int j = 1; j <= m; ++j) { if (column[j].size() == 1) { cur.push_back({j, 1}); used_column[j] = false; pair<char, int> t = *(column[j].begin()); add(ans, 'K', j, t.first); } } int head = 0, tail = cur.size(); while (head < tail) { pair<int, int> x = cur[head]; if (x.second == 0) { int i = x.first; used_row[i] = false; for (int j = 1; j <= m; ++j) { if (a[i][j] == '.') continue; --column[j][a[i][j]]; if (column[j][a[i][j]] == 0) column[j].erase(a[i][j]); if (column[j].size() == 1 && used_column[j]) { used_column[j] = false; cur.push_back({j, 1}); pair<char, int> t = *(column[j].begin()); add(ans, 'K', j, t.first); ++tail; } a[i][j] = '.'; } } else { int j = x.first; used_column[j] = false; for (int i = 1; i <= n; ++i) { if (a[i][j] == '.') continue; --row[i][a[i][j]]; if (row[i][a[i][j]] == 0) row[i].erase(a[i][j]); if (row[i].size() == 1 && used_row[i]) { used_row[i] = false; cur.push_back({i, 0}); pair<char, int> t = *(row[i].begin()); add(ans, 'R', i, t.first); ++tail; } a[i][j] = '.'; } } ++head; } reverse(ans.begin(), ans.end()); cout << ans.size() << endl; for (const auto& x : ans) { cout << x.c << " " << x.x << " " << x.letter << endl; } return 0; } |