#include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define ld long double #define pb push_back #define nd second #define st first #define sz size #define forr(i, n) for(int i=1;i<=n;i++) const ll infl=1e18+90; const int inf=1e9+90; const int roz=2e3+93; deque<pair<int,pair<int,int> > > kolej, ruchy; int tab[roz][roz], kol[roz][30], wie[roz][30], zap_kol[roz], zap_wie[roz]; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin>>n>>m; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { char a; cin>>a; int s=(int)(a-'A'); tab[i][j]=s; wie[i][s]++; kol[j][s]++; } } for(int i=1;i<=m;i++) { for(int j=0;j<26;j++) { if(kol[i][j]==n) { kolej.pb({i, {j, 1}}); zap_kol[i]=1; } } } for(int i=1;i<=n;i++) { for(int j=0;j<26;j++) { if(wie[i][j]==m) { zap_wie[i]=1; kolej.pb({i, {j, 0}}); } } } while(!kolej.empty()) { pair<int,pair<int,int> > p=kolej.front(); kolej.pop_front(); int num=p.st, lit=p.nd.st, z=p.nd.nd; ruchy.pb(p); //cerr<<"wchodze dla num "<<num<<" lit = "<<lit<<" z = "<<z<<"\n"; if(z==0) { for(int j=1;j<=m;j++) { int x=tab[num][j]; if(x==-1) continue; else { for(int i=0;i<26;i++) { if(x!=i) { kol[j][i]++; if(kol[j][i]==n&&zap_kol[j]==0) { kolej.pb({j, {i, 1}}); zap_kol[j]=1; } } } } } } else { for(int j=1;j<=n;j++) { int x=tab[j][num]; if(x==-1) continue; else { for(int i=0;i<26;i++) { if(x!=i) { wie[j][i]++; if(wie[j][i]==m&&zap_wie[j]==0) { kolej.pb({j, {i, 0}}); zap_wie[j]=1; } } } } } } } reverse(ruchy.begin(), ruchy.end()); cout<<ruchy.size()<<"\n"; for(auto [a, b]:ruchy) { if(b.nd==1) cout<<"K "; else cout<<"R "; cout<<a<<" "<<(char)('A'+b.st)<<"\n"; } }
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 | #include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define ld long double #define pb push_back #define nd second #define st first #define sz size #define forr(i, n) for(int i=1;i<=n;i++) const ll infl=1e18+90; const int inf=1e9+90; const int roz=2e3+93; deque<pair<int,pair<int,int> > > kolej, ruchy; int tab[roz][roz], kol[roz][30], wie[roz][30], zap_kol[roz], zap_wie[roz]; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin>>n>>m; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { char a; cin>>a; int s=(int)(a-'A'); tab[i][j]=s; wie[i][s]++; kol[j][s]++; } } for(int i=1;i<=m;i++) { for(int j=0;j<26;j++) { if(kol[i][j]==n) { kolej.pb({i, {j, 1}}); zap_kol[i]=1; } } } for(int i=1;i<=n;i++) { for(int j=0;j<26;j++) { if(wie[i][j]==m) { zap_wie[i]=1; kolej.pb({i, {j, 0}}); } } } while(!kolej.empty()) { pair<int,pair<int,int> > p=kolej.front(); kolej.pop_front(); int num=p.st, lit=p.nd.st, z=p.nd.nd; ruchy.pb(p); //cerr<<"wchodze dla num "<<num<<" lit = "<<lit<<" z = "<<z<<"\n"; if(z==0) { for(int j=1;j<=m;j++) { int x=tab[num][j]; if(x==-1) continue; else { for(int i=0;i<26;i++) { if(x!=i) { kol[j][i]++; if(kol[j][i]==n&&zap_kol[j]==0) { kolej.pb({j, {i, 1}}); zap_kol[j]=1; } } } } } } else { for(int j=1;j<=n;j++) { int x=tab[j][num]; if(x==-1) continue; else { for(int i=0;i<26;i++) { if(x!=i) { wie[j][i]++; if(wie[j][i]==m&&zap_wie[j]==0) { kolej.pb({j, {i, 0}}); zap_wie[j]=1; } } } } } } } reverse(ruchy.begin(), ruchy.end()); cout<<ruchy.size()<<"\n"; for(auto [a, b]:ruchy) { if(b.nd==1) cout<<"K "; else cout<<"R "; cout<<a<<" "<<(char)('A'+b.st)<<"\n"; } } |