#include <bits/stdc++.h> using namespace std; const int MAXN = 2e3+7; const int ALF = 26; int col[MAXN][ALF]; int row[MAXN][ALF]; int mat[MAXN][MAXN]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int h, w; cin >> h >> w; for(int i = 0; i < h; i++){ string s; cin >> s; for(int j = 0; j < w; j++){ row[i][s[j]-'A']++; col[j][s[j]-'A']++; mat[i][j]=s[j]-'A'; } } vector<string>res; while(1){ bool found = false; for(int i = 0; i < h; i++){ int el = -1; for(int j = 0; j < ALF; j++) if(row[i][j]>0){ if(el!=-1){ el=-1; break; } el=j; } if(el!=-1){ res.push_back("R "+to_string(i+1)+" "+char(el+'A')); row[i][el]=0; for(int j = 0; j < w; j++){ if(mat[i][j]==el){ mat[i][j]=-1; col[j][el]--; } } found=true; break; } } if(found) continue; for(int i = 0; i < w; i++){ int el = -1; for(int j = 0; j < ALF; j++) if(col[i][j]>0){ if(el!=-1){ el=-1; break; } el=j; } if(el!=-1){ res.push_back("K "+to_string(i+1)+" "+char(el+'A')); col[i][el]=0; for(int j = 0; j < h; j++){ if(mat[j][i]==el){ mat[j][i]=-1; row[j][el]--; } } found=true; break; } } if(found) continue; break; } cout << res.size() << '\n'; for(int i = (int)res.size()-1; i>=0; i--) cout << res[i] << '\n'; 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 | #include <bits/stdc++.h> using namespace std; const int MAXN = 2e3+7; const int ALF = 26; int col[MAXN][ALF]; int row[MAXN][ALF]; int mat[MAXN][MAXN]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int h, w; cin >> h >> w; for(int i = 0; i < h; i++){ string s; cin >> s; for(int j = 0; j < w; j++){ row[i][s[j]-'A']++; col[j][s[j]-'A']++; mat[i][j]=s[j]-'A'; } } vector<string>res; while(1){ bool found = false; for(int i = 0; i < h; i++){ int el = -1; for(int j = 0; j < ALF; j++) if(row[i][j]>0){ if(el!=-1){ el=-1; break; } el=j; } if(el!=-1){ res.push_back("R "+to_string(i+1)+" "+char(el+'A')); row[i][el]=0; for(int j = 0; j < w; j++){ if(mat[i][j]==el){ mat[i][j]=-1; col[j][el]--; } } found=true; break; } } if(found) continue; for(int i = 0; i < w; i++){ int el = -1; for(int j = 0; j < ALF; j++) if(col[i][j]>0){ if(el!=-1){ el=-1; break; } el=j; } if(el!=-1){ res.push_back("K "+to_string(i+1)+" "+char(el+'A')); col[i][el]=0; for(int j = 0; j < h; j++){ if(mat[j][i]==el){ mat[j][i]=-1; row[j][el]--; } } found=true; break; } } if(found) continue; break; } cout << res.size() << '\n'; for(int i = (int)res.size()-1; i>=0; i--) cout << res[i] << '\n'; return 0; } |