#include <bits/stdc++.h> #define int long long #define pii pair<int,int> #define all(x) x.begin(),x.end() #define vi vector<int> #define vii vector<pii> #define vb vector<bool> #define siz(x) (int)x.size() #define pb push_back #define nd second #define st first #define rep(i,a,b) for(int i=a; i<=b; i++) using namespace std; const int maxn = 1e6, inf = 1e9; struct ruch{char typ; int nr; char kolor;}; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin>>n>>m; vector<vi> obrazek(n,vi(m)); vector<vi>wiersze(n,vi(26,0)); vector<vi>kolumny(m,vi(26,0)); vi sumawiersza(n,m); vi sumakolumny(m,n); int suma = n*m; rep(i,0,n-1){ rep(j,0,m-1){ char c; cin>>c; int akt = (int)(c-'A'); obrazek[i][j] = akt; wiersze[i][akt]++; kolumny[j][akt]++; } } vector<ruch>ans; while(suma>0){ rep(i,0,n-1){ if(sumawiersza[i]){ int kolor=-1; rep(j,0,25){ if(kolor!= -1 && wiersze[i][j]){kolor=-5;break;} if(wiersze[i][j] && kolor == -1)kolor = j; } if(kolor==-5)continue; sumawiersza[i] = 0; suma-=wiersze[i][kolor]; ans.pb({'R',i+1,(char)('A'+kolor)}); rep(j,0,m-1){ if(obrazek[i][j]!=-1){ if(kolumny[j][kolor]>0) kolumny[j][kolor]--; obrazek[i][j] = -1; if(sumakolumny[j]>0) sumakolumny[j]--; } } } } rep(i,0,m-1){ if(sumakolumny[i]){ int kolor=-1; rep(j,0,25){ if(kolor!= -1 && kolumny[i][j]){kolor=-5;break;} if(kolumny[i][j] && kolor == -1)kolor = j; } if(kolor==-5)continue; sumakolumny[i] = 0; suma-=kolumny[i][kolor]; ans.pb({'K',i+1,(char)('A'+kolor)}); rep(j,0,n-1){ if(obrazek[j][i]!=-1){ if(wiersze[j][kolor]>0) wiersze[j][kolor]--; obrazek[j][i] = -1; if(sumawiersza[j]>0) sumawiersza[j]--; } } } } } cout<<siz(ans)<<'\n'; reverse(all(ans)); for(auto [a,b,c] : ans){ cout<<a<<' '<<b<<' '<<c<<'\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 | #include <bits/stdc++.h> #define int long long #define pii pair<int,int> #define all(x) x.begin(),x.end() #define vi vector<int> #define vii vector<pii> #define vb vector<bool> #define siz(x) (int)x.size() #define pb push_back #define nd second #define st first #define rep(i,a,b) for(int i=a; i<=b; i++) using namespace std; const int maxn = 1e6, inf = 1e9; struct ruch{char typ; int nr; char kolor;}; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin>>n>>m; vector<vi> obrazek(n,vi(m)); vector<vi>wiersze(n,vi(26,0)); vector<vi>kolumny(m,vi(26,0)); vi sumawiersza(n,m); vi sumakolumny(m,n); int suma = n*m; rep(i,0,n-1){ rep(j,0,m-1){ char c; cin>>c; int akt = (int)(c-'A'); obrazek[i][j] = akt; wiersze[i][akt]++; kolumny[j][akt]++; } } vector<ruch>ans; while(suma>0){ rep(i,0,n-1){ if(sumawiersza[i]){ int kolor=-1; rep(j,0,25){ if(kolor!= -1 && wiersze[i][j]){kolor=-5;break;} if(wiersze[i][j] && kolor == -1)kolor = j; } if(kolor==-5)continue; sumawiersza[i] = 0; suma-=wiersze[i][kolor]; ans.pb({'R',i+1,(char)('A'+kolor)}); rep(j,0,m-1){ if(obrazek[i][j]!=-1){ if(kolumny[j][kolor]>0) kolumny[j][kolor]--; obrazek[i][j] = -1; if(sumakolumny[j]>0) sumakolumny[j]--; } } } } rep(i,0,m-1){ if(sumakolumny[i]){ int kolor=-1; rep(j,0,25){ if(kolor!= -1 && kolumny[i][j]){kolor=-5;break;} if(kolumny[i][j] && kolor == -1)kolor = j; } if(kolor==-5)continue; sumakolumny[i] = 0; suma-=kolumny[i][kolor]; ans.pb({'K',i+1,(char)('A'+kolor)}); rep(j,0,n-1){ if(obrazek[j][i]!=-1){ if(wiersze[j][kolor]>0) wiersze[j][kolor]--; obrazek[j][i] = -1; if(sumawiersza[j]>0) sumawiersza[j]--; } } } } } cout<<siz(ans)<<'\n'; reverse(all(ans)); for(auto [a,b,c] : ans){ cout<<a<<' '<<b<<' '<<c<<'\n'; } } |