#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e3 * 2 + 10, MAXA = 1e3 * 4 + 10;
int cntr[MAX][26], cntc[MAX][26];
string s[MAX];
struct moves{
int id;
char pos, color;
};
bool doner[MAX], donec[MAX];
queue<moves>Q;
moves ans[MAXA];
void addr(int id){
//cout << id << endl;
if (doner[id]){
return;
}
pair<int,int>tmp = {-1,-1};
bool ok = false;
for (int x = 0; x < 26; x++){
if (cntr[id][x] > 0){
if (tmp.first >= 0){
ok = false;
break;
}
ok = true;
tmp = {id,x};
}
}
//cout << tmp.first << " " << tmp.second << " " << ok << endl;
if (ok){
moves tmp2;
tmp2.id = id;
tmp2.color = char(tmp.second+'A');
tmp2.pos = 'R';
Q.push({tmp2});
doner[id] = true;
}
}
void addc(int id){
//cout << id << endl;
if (donec[id]){
return;
}
pair<int,int>tmp = {-1,-1};
bool ok = false;
for (int x = 0; x < 26; x++){
if (cntc[id][x] > 0){
if (tmp.first >= 0){
ok = false;
break;
}
ok = true;
tmp = {id,x};
}
}
//cout << tmp.first << " " << tmp.second << " " << ok << endl;
if (ok){
moves tmp2;
tmp2.id = id;
tmp2.color = char(tmp.second+'A');
tmp2.pos = 'K';
Q.push({tmp2});
donec[id] = true;
}
}
void solve(int n, int m){
int idx = 0;
for (int i = 0; i < n; i++){
addr(i);
}
for (int i = 0; i < m; i++){
addc(i);
}
while (!Q.empty()){
moves cur = Q.front();
Q.pop();
ans[idx] = cur;
idx++;
if (cur.pos == 'R'){
for (int i = 0; i < m; i++){
cntc[i][s[cur.id][i]-'A']--;
addc(i);
}
} else {
for (int i = 0; i < n; i++){
cntr[i][s[i][cur.id]-'A']--;
addr(i);
}
}
}
reverse(ans,ans+idx);
cout << idx << "\n";
for (int i = 0; i < idx; i++){
cout << ans[i].pos << " " << ans[i].id + 1 << " " << ans[i].color << "\n";
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++){
cin >> s[i];
for (int j = 0; j < m; j++){
char x = s[i][j];
cntr[i][x-'A']++;
cntc[j][x-'A']++;
}
}
solve(n,m);
}
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 <bits/stdc++.h> using namespace std; const int MAX = 1e3 * 2 + 10, MAXA = 1e3 * 4 + 10; int cntr[MAX][26], cntc[MAX][26]; string s[MAX]; struct moves{ int id; char pos, color; }; bool doner[MAX], donec[MAX]; queue<moves>Q; moves ans[MAXA]; void addr(int id){ //cout << id << endl; if (doner[id]){ return; } pair<int,int>tmp = {-1,-1}; bool ok = false; for (int x = 0; x < 26; x++){ if (cntr[id][x] > 0){ if (tmp.first >= 0){ ok = false; break; } ok = true; tmp = {id,x}; } } //cout << tmp.first << " " << tmp.second << " " << ok << endl; if (ok){ moves tmp2; tmp2.id = id; tmp2.color = char(tmp.second+'A'); tmp2.pos = 'R'; Q.push({tmp2}); doner[id] = true; } } void addc(int id){ //cout << id << endl; if (donec[id]){ return; } pair<int,int>tmp = {-1,-1}; bool ok = false; for (int x = 0; x < 26; x++){ if (cntc[id][x] > 0){ if (tmp.first >= 0){ ok = false; break; } ok = true; tmp = {id,x}; } } //cout << tmp.first << " " << tmp.second << " " << ok << endl; if (ok){ moves tmp2; tmp2.id = id; tmp2.color = char(tmp.second+'A'); tmp2.pos = 'K'; Q.push({tmp2}); donec[id] = true; } } void solve(int n, int m){ int idx = 0; for (int i = 0; i < n; i++){ addr(i); } for (int i = 0; i < m; i++){ addc(i); } while (!Q.empty()){ moves cur = Q.front(); Q.pop(); ans[idx] = cur; idx++; if (cur.pos == 'R'){ for (int i = 0; i < m; i++){ cntc[i][s[cur.id][i]-'A']--; addc(i); } } else { for (int i = 0; i < n; i++){ cntr[i][s[i][cur.id]-'A']--; addr(i); } } } reverse(ans,ans+idx); cout << idx << "\n"; for (int i = 0; i < idx; i++){ cout << ans[i].pos << " " << ans[i].id + 1 << " " << ans[i].color << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++){ cin >> s[i]; for (int j = 0; j < m; j++){ char x = s[i][j]; cntr[i][x-'A']++; cntc[j][x-'A']++; } } solve(n,m); } |
English