#include <bits/stdc++.h> #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; int inf=1000000007; ll infl=1000000000000000007; ll mod=1000000007; ll mod1=998244353; const int N=2007; int g[N][N]; int R[N][27]; int C[N][27]; int ileR[N]; int ileC[N]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { char c; cin>>c; g[i][j]=c-'A'+1; R[i][g[i][j]]++; C[j][g[i][j]]++; } } deque<pair<int,int>>Q; for(int i=1;i<=n;i++) { for(int j=1;j<=26;j++) if(R[i][j]) ileR[i]++; if(ileR[i]==1) Q.pb({i,0}); } for(int i=1;i<=m;i++) { for(int j=1;j<=26;j++) if(C[i][j]) ileC[i]++; if(ileC[i]==1) Q.pb({i,1}); } vector<pair<pair<char,int>,char>>ans; while(sz(Q)>0) { int k=Q[0].st,t=Q[0].nd; Q.pop_front(); if(t==0) { int xd=1; for(int j=1;j<=26;j++) if(R[k][j]) xd=j; ans.pb({{'R',k},xd+'A'-1}); for(int i=1;i<=m;i++) { if(g[k][i]) { C[i][g[k][i]]--; if(C[i][g[k][i]]==0) { ileC[i]--; if(ileC[i]==1) Q.pb({i,1}); } g[k][i]=0; } } } else { int xd=1; for(int j=1;j<=26;j++) if(C[k][j]) xd=j; ans.pb({{'K',k},xd+'A'-1}); for(int i=1;i<=n;i++) { if(g[i][k]) { R[i][g[i][k]]--; if(R[i][g[i][k]]==0) { ileR[i]--; if(ileR[i]==1) Q.pb({i,0}); } g[i][k]=0; } } } } reverse(all(ans)); cout<<sz(ans)<<endl; for(auto [p,x]:ans) cout<<p.st<<" "<<p.nd<<" "<<x<<endl; 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 | #include <bits/stdc++.h> #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; int inf=1000000007; ll infl=1000000000000000007; ll mod=1000000007; ll mod1=998244353; const int N=2007; int g[N][N]; int R[N][27]; int C[N][27]; int ileR[N]; int ileC[N]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { char c; cin>>c; g[i][j]=c-'A'+1; R[i][g[i][j]]++; C[j][g[i][j]]++; } } deque<pair<int,int>>Q; for(int i=1;i<=n;i++) { for(int j=1;j<=26;j++) if(R[i][j]) ileR[i]++; if(ileR[i]==1) Q.pb({i,0}); } for(int i=1;i<=m;i++) { for(int j=1;j<=26;j++) if(C[i][j]) ileC[i]++; if(ileC[i]==1) Q.pb({i,1}); } vector<pair<pair<char,int>,char>>ans; while(sz(Q)>0) { int k=Q[0].st,t=Q[0].nd; Q.pop_front(); if(t==0) { int xd=1; for(int j=1;j<=26;j++) if(R[k][j]) xd=j; ans.pb({{'R',k},xd+'A'-1}); for(int i=1;i<=m;i++) { if(g[k][i]) { C[i][g[k][i]]--; if(C[i][g[k][i]]==0) { ileC[i]--; if(ileC[i]==1) Q.pb({i,1}); } g[k][i]=0; } } } else { int xd=1; for(int j=1;j<=26;j++) if(C[k][j]) xd=j; ans.pb({{'K',k},xd+'A'-1}); for(int i=1;i<=n;i++) { if(g[i][k]) { R[i][g[i][k]]--; if(R[i][g[i][k]]==0) { ileR[i]--; if(ileR[i]==1) Q.pb({i,0}); } g[i][k]=0; } } } } reverse(all(ans)); cout<<sz(ans)<<endl; for(auto [p,x]:ans) cout<<p.st<<" "<<p.nd<<" "<<x<<endl; return 0; } |