#include <bits/stdc++.h> using namespace std; struct op { char typ; int nr; char kol; }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector <vector <char> > v(n, vector <char> (m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> v[i][j]; } } vector <op> res; vector <map <char, int> > ile(n+m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ile[i][v[i][j]]++; ile[j+n][v[i][j]]++; } } queue <int> q; for (int i = 0; i < n+m; i++) { if (ile[i].size() == 1) { q.push(i); } } while (!q.empty()) { op temp; int cr = q.front(); q.pop(); if (cr >= n) { if (ile[cr].empty()) continue; temp.typ = 'K'; temp.nr = cr-n; temp.kol = (*ile[cr].begin()).first; res.push_back(temp); cr -= n; for (int i = 0; i < n; i++) { if (v[i][cr] != 'x') { ile[i][v[i][cr]]--; if (ile[i][v[i][cr]] == 0) { ile[i].erase(ile[i].find(v[i][cr])); if (ile[i].size() == 1) q.push(i); } ile[cr+n][v[i][cr]]--; if (ile[cr+n][v[i][cr]] == 0) { ile[cr+n].erase(ile[cr+n].find(v[i][cr])); if (ile[cr+n].size() == 1) q.push(cr+n); } v[i][cr] = 'x'; } } } else { if (ile[cr].empty()) continue; temp.typ = 'R'; temp.nr = cr; temp.kol = (*ile[cr].begin()).first; res.push_back(temp); for (int j = 0; j < m; j++) { if (v[cr][j] != 'x') { ile[cr][v[cr][j]]--; if (ile[cr][v[cr][j]] == 0) { ile[cr].erase(ile[cr].find(v[cr][j])); if (ile[cr].size() == 1) q.push(cr); } ile[j+n][v[cr][j]]--; if (ile[j+n][v[cr][j]] == 0) { ile[j+n].erase(ile[j+n].find(v[cr][j])); if (ile[j+n].size() == 1) q.push(j+n); } v[cr][j] = 'x'; } } } } cout << (int)res.size() << "\n"; for (int i = (int)res.size()-1; i >= 0; i--) { cout << res[i].typ << " " << res[i].nr+1 << " " << res[i].kol << "\n"; } }
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 | #include <bits/stdc++.h> using namespace std; struct op { char typ; int nr; char kol; }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector <vector <char> > v(n, vector <char> (m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> v[i][j]; } } vector <op> res; vector <map <char, int> > ile(n+m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ile[i][v[i][j]]++; ile[j+n][v[i][j]]++; } } queue <int> q; for (int i = 0; i < n+m; i++) { if (ile[i].size() == 1) { q.push(i); } } while (!q.empty()) { op temp; int cr = q.front(); q.pop(); if (cr >= n) { if (ile[cr].empty()) continue; temp.typ = 'K'; temp.nr = cr-n; temp.kol = (*ile[cr].begin()).first; res.push_back(temp); cr -= n; for (int i = 0; i < n; i++) { if (v[i][cr] != 'x') { ile[i][v[i][cr]]--; if (ile[i][v[i][cr]] == 0) { ile[i].erase(ile[i].find(v[i][cr])); if (ile[i].size() == 1) q.push(i); } ile[cr+n][v[i][cr]]--; if (ile[cr+n][v[i][cr]] == 0) { ile[cr+n].erase(ile[cr+n].find(v[i][cr])); if (ile[cr+n].size() == 1) q.push(cr+n); } v[i][cr] = 'x'; } } } else { if (ile[cr].empty()) continue; temp.typ = 'R'; temp.nr = cr; temp.kol = (*ile[cr].begin()).first; res.push_back(temp); for (int j = 0; j < m; j++) { if (v[cr][j] != 'x') { ile[cr][v[cr][j]]--; if (ile[cr][v[cr][j]] == 0) { ile[cr].erase(ile[cr].find(v[cr][j])); if (ile[cr].size() == 1) q.push(cr); } ile[j+n][v[cr][j]]--; if (ile[j+n][v[cr][j]] == 0) { ile[j+n].erase(ile[j+n].find(v[cr][j])); if (ile[j+n].size() == 1) q.push(j+n); } v[cr][j] = 'x'; } } } } cout << (int)res.size() << "\n"; for (int i = (int)res.size()-1; i >= 0; i--) { cout << res[i].typ << " " << res[i].nr+1 << " " << res[i].kol << "\n"; } } |