#include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; typedef pair<long long,int> PLI; struct Event{ char type; int ind; int let; }; int R[2005][30]; int C[2005][30]; int board[2005][2005]; int cntR[2005]; int cntC[2005]; int main(){ ios_base::sync_with_stdio(0); int n, m; cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ char x; cin>>x; board[i][j] = x-'A'; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ R[i][board[i][j]]++; C[j][board[i][j]]++; } } for(int i=1;i<=n;i++){ for(int j=0;j<26;j++){ if(R[i][j]) cntR[i]++; } } for(int i=1;i<=m;i++){ for(int j=0;j<26;j++){ if(C[i][j]) cntC[i]++; } } queue<Event> Q; vector<Event> res; for(int i=1;i<=n;i++){ int let = -1; for(int j=0;j<26;j++) if(R[i][j] != 0) let = j; if(cntR[i] <= 1 && let != -1) Q.push({'R', i, let}); } for(int i=1;i<=m;i++){ int let = -1; for(int j=0;j<26;j++) if(C[i][j] != 0) let = j; if(cntC[i] <= 1 && let != -1) Q.push({'C', i, let}); } while(!Q.empty()){ Event ev = Q.front(); Q.pop(); res.push_back(ev); if(ev.type == 'R'){ for(int i=1;i<=m;i++){ if(board[ev.ind][i] != -1){ C[i][board[ev.ind][i]]--; if(C[i][board[ev.ind][i]] == 0){ cntC[i]--; if(cntC[i] == 1){ int let = -1; for(int j=0;j<26;j++) if(C[i][j] != 0) let = j; if(let != -1) Q.push({'C', i, let}); } } board[ev.ind][i] = -1; } } } else{ for(int i=1;i<=n;i++){ if(board[i][ev.ind] != -1){ R[i][board[i][ev.ind]]--; if(R[i][board[i][ev.ind]] == 0){ cntR[i]--; if(cntR[i] == 1){ int let = -1; for(int j=0;j<26;j++) if(R[i][j] != 0) let = j; if(let != -1) Q.push({'R', i, let}); } } board[i][ev.ind] = -1; } } } } reverse(res.begin(), res.end()); cout<<res.size()<<endl; for(Event ev: res){ if(ev.type == 'C') ev.type = 'K'; cout<<ev.type<<" "<<ev.ind<<" "<<(char)(ev.let+'A')<<endl; } }
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 | #include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; typedef pair<long long,int> PLI; struct Event{ char type; int ind; int let; }; int R[2005][30]; int C[2005][30]; int board[2005][2005]; int cntR[2005]; int cntC[2005]; int main(){ ios_base::sync_with_stdio(0); int n, m; cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ char x; cin>>x; board[i][j] = x-'A'; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ R[i][board[i][j]]++; C[j][board[i][j]]++; } } for(int i=1;i<=n;i++){ for(int j=0;j<26;j++){ if(R[i][j]) cntR[i]++; } } for(int i=1;i<=m;i++){ for(int j=0;j<26;j++){ if(C[i][j]) cntC[i]++; } } queue<Event> Q; vector<Event> res; for(int i=1;i<=n;i++){ int let = -1; for(int j=0;j<26;j++) if(R[i][j] != 0) let = j; if(cntR[i] <= 1 && let != -1) Q.push({'R', i, let}); } for(int i=1;i<=m;i++){ int let = -1; for(int j=0;j<26;j++) if(C[i][j] != 0) let = j; if(cntC[i] <= 1 && let != -1) Q.push({'C', i, let}); } while(!Q.empty()){ Event ev = Q.front(); Q.pop(); res.push_back(ev); if(ev.type == 'R'){ for(int i=1;i<=m;i++){ if(board[ev.ind][i] != -1){ C[i][board[ev.ind][i]]--; if(C[i][board[ev.ind][i]] == 0){ cntC[i]--; if(cntC[i] == 1){ int let = -1; for(int j=0;j<26;j++) if(C[i][j] != 0) let = j; if(let != -1) Q.push({'C', i, let}); } } board[ev.ind][i] = -1; } } } else{ for(int i=1;i<=n;i++){ if(board[i][ev.ind] != -1){ R[i][board[i][ev.ind]]--; if(R[i][board[i][ev.ind]] == 0){ cntR[i]--; if(cntR[i] == 1){ int let = -1; for(int j=0;j<26;j++) if(R[i][j] != 0) let = j; if(let != -1) Q.push({'R', i, let}); } } board[i][ev.ind] = -1; } } } } reverse(res.begin(), res.end()); cout<<res.size()<<endl; for(Event ev: res){ if(ev.type == 'C') ev.type = 'K'; cout<<ev.type<<" "<<ev.ind<<" "<<(char)(ev.let+'A')<<endl; } } |