#include <bits/stdc++.h> using namespace std; const int inf=1e9; const int MAXN=2005; int c[MAXN][MAXN]; int colcnt[MAXN][26]; int rowcnt[MAXN][26]; int colnonzero[MAXN]; int rownonzero[MAXN]; int n,m; struct info{ char type; char letter; int idx; }; int main() { cin>>n>>m; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { char ch; cin>>ch; c[i][j]=ch-'A'; rowcnt[i][ch-'A']++; if(rowcnt[i][ch-'A']==1) { rownonzero[i]++; } colcnt[j][ch-'A']++; if(colcnt[j][ch-'A']==1) { colnonzero[j]++; } } } vector<info>ans; queue<info>q; for(int i=1; i<=n; i++) { if(rownonzero[i]==1) { char ch; for(int j=0; j<26; j++) { if(rowcnt[i][j]!=0) { ch=j+'A'; } } ans.push_back({'R', ch,i}); q.push(ans.back()); rownonzero[i]=inf; } } for(int i=1; i<=m; i++) { if(colnonzero[i]==1) { char ch; for(int j=0; j<26; j++) { if(colcnt[i][j]!=0) { ch=j+'A'; } } ans.push_back({'K', ch,i}); q.push(ans.back()); colnonzero[i]=inf; } } while(!q.empty()) { info cur=q.front(); q.pop(); if(cur.type=='R') { for(int i=1; i<=m; i++) { colcnt[i][int(cur.letter-'A')]--; if(colcnt[i][int(cur.letter-'A')]==0) { colnonzero[i]--; } if(colnonzero[i]==1) { char ch; colnonzero[i]=inf; for(int j=0; j<26; j++) { if(colcnt[i][j]!=0) { ch=j+'A'; } } q.push({'K', ch, i}); ans.push_back({'K', ch, i}); } } } else { for(int i=1; i<=n; i++) { rowcnt[i][int(cur.letter-'A')]--; if(rowcnt[i][int(cur.letter-'A')]==0) { rownonzero[i]--; } if(rownonzero[i]==1) { char ch; rownonzero[i]=inf; for(int j=0; j<26; j++) { if(rowcnt[i][j]!=0) { ch=j+'A'; } } q.push({'R', ch, i}); ans.push_back({'R', ch, i}); } } } } reverse(ans.begin(), ans.end()); cout<<ans.size()<<"\n"; for(auto i : ans) { cout<<i.type<<" "<<i.idx<<" "<<i.letter<<"\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 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #include <bits/stdc++.h> using namespace std; const int inf=1e9; const int MAXN=2005; int c[MAXN][MAXN]; int colcnt[MAXN][26]; int rowcnt[MAXN][26]; int colnonzero[MAXN]; int rownonzero[MAXN]; int n,m; struct info{ char type; char letter; int idx; }; int main() { cin>>n>>m; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { char ch; cin>>ch; c[i][j]=ch-'A'; rowcnt[i][ch-'A']++; if(rowcnt[i][ch-'A']==1) { rownonzero[i]++; } colcnt[j][ch-'A']++; if(colcnt[j][ch-'A']==1) { colnonzero[j]++; } } } vector<info>ans; queue<info>q; for(int i=1; i<=n; i++) { if(rownonzero[i]==1) { char ch; for(int j=0; j<26; j++) { if(rowcnt[i][j]!=0) { ch=j+'A'; } } ans.push_back({'R', ch,i}); q.push(ans.back()); rownonzero[i]=inf; } } for(int i=1; i<=m; i++) { if(colnonzero[i]==1) { char ch; for(int j=0; j<26; j++) { if(colcnt[i][j]!=0) { ch=j+'A'; } } ans.push_back({'K', ch,i}); q.push(ans.back()); colnonzero[i]=inf; } } while(!q.empty()) { info cur=q.front(); q.pop(); if(cur.type=='R') { for(int i=1; i<=m; i++) { colcnt[i][int(cur.letter-'A')]--; if(colcnt[i][int(cur.letter-'A')]==0) { colnonzero[i]--; } if(colnonzero[i]==1) { char ch; colnonzero[i]=inf; for(int j=0; j<26; j++) { if(colcnt[i][j]!=0) { ch=j+'A'; } } q.push({'K', ch, i}); ans.push_back({'K', ch, i}); } } } else { for(int i=1; i<=n; i++) { rowcnt[i][int(cur.letter-'A')]--; if(rowcnt[i][int(cur.letter-'A')]==0) { rownonzero[i]--; } if(rownonzero[i]==1) { char ch; rownonzero[i]=inf; for(int j=0; j<26; j++) { if(rowcnt[i][j]!=0) { ch=j+'A'; } } q.push({'R', ch, i}); ans.push_back({'R', ch, i}); } } } } reverse(ans.begin(), ans.end()); cout<<ans.size()<<"\n"; for(auto i : ans) { cout<<i.type<<" "<<i.idx<<" "<<i.letter<<"\n"; } return 0; } |