#include<bits/stdc++.h>
using namespace std;
struct op {
int type, id;
char c;
op() {}
op(int type, int id, char c) : type(type), id(id), c(c) {}
};
void solve() {
int n, m; cin >> n >> m; // n = height, m = width
vector<map<char, int>> h(n), w(m);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
char c; cin >> c;
h[i][c]++;
w[j][c]++;
}
}
vector<int> vis_h(n, 0), vis_w(m, 0);
queue<pair<int, int>> q;
for(int i = 0; i < n; i++) {
if(h[i].size() == 1) {
q.push({0, i});
vis_h[i] = 1;
}
}
for(int i = 0; i < m; i++) {
if(w[i].size() == 1) {
q.push({1, i});
vis_w[i] = 1;
}
}
vector<op> moves;
while(!q.empty()) {
int type = q.front().first, id = q.front().second;
q.pop();
char c;
if(type == 0) {
if(h[id].empty()) continue;
c = h[id].begin()->first;
for(int i = 0; i < m; i++) {
w[i][c]--;
if(w[i][c] == 0) {
w[i].erase(c);
}
if(w[i].size() == 1 && vis_w[i] == 0) {
q.push({1, i});
vis_w[i] = 1;
}
}
} else {
if(w[id].empty()) continue;
c = w[id].begin()->first;
for(int i = 0; i < n; i++) {
h[i][c]--;
if(h[i][c] == 0) {
h[i].erase(c);
}
if(h[i].size() == 1 && vis_h[i] == 0) {
q.push({0, i});
vis_h[i] = 1;
}
}
}
moves.emplace_back(type, id, c);
}
reverse(moves.begin(), moves.end());
cout << moves.size() << "\n";
for(int i = 0; i < moves.size(); i++) {
cout << (moves[i].type == 0 ? "R" : "K") << " " << moves[i].id + 1 << " " << moves[i].c << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
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 | #include<bits/stdc++.h> using namespace std; struct op { int type, id; char c; op() {} op(int type, int id, char c) : type(type), id(id), c(c) {} }; void solve() { int n, m; cin >> n >> m; // n = height, m = width vector<map<char, int>> h(n), w(m); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { char c; cin >> c; h[i][c]++; w[j][c]++; } } vector<int> vis_h(n, 0), vis_w(m, 0); queue<pair<int, int>> q; for(int i = 0; i < n; i++) { if(h[i].size() == 1) { q.push({0, i}); vis_h[i] = 1; } } for(int i = 0; i < m; i++) { if(w[i].size() == 1) { q.push({1, i}); vis_w[i] = 1; } } vector<op> moves; while(!q.empty()) { int type = q.front().first, id = q.front().second; q.pop(); char c; if(type == 0) { if(h[id].empty()) continue; c = h[id].begin()->first; for(int i = 0; i < m; i++) { w[i][c]--; if(w[i][c] == 0) { w[i].erase(c); } if(w[i].size() == 1 && vis_w[i] == 0) { q.push({1, i}); vis_w[i] = 1; } } } else { if(w[id].empty()) continue; c = w[id].begin()->first; for(int i = 0; i < n; i++) { h[i][c]--; if(h[i][c] == 0) { h[i].erase(c); } if(h[i].size() == 1 && vis_h[i] == 0) { q.push({0, i}); vis_h[i] = 1; } } } moves.emplace_back(type, id, c); } reverse(moves.begin(), moves.end()); cout << moves.size() << "\n"; for(int i = 0; i < moves.size(); i++) { cout << (moves[i].type == 0 ? "R" : "K") << " " << moves[i].id + 1 << " " << moves[i].c << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; } |
English