#include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; typedef long long ll; typedef pair<ll,ll> pll; typedef pair<ll,int> pli; typedef pair<int,ll> pil; typedef pair<int,int> pii; const ll INFLL=1e18+7; const int INF=1e9+7; #define pb push_back const int MAXN = 2e3+7, A = 26; int arr[MAXN][MAXN]; int cntR[A][MAXN], cntC[A][MAXN]; int diffR[MAXN], diffC[MAXN]; int main() { ios_base::sync_with_stdio(0); int n,m; cin>>n>>m; for(int i=1;i<=n;++i){ string s; cin>>s; for(int j=0;j<m;++j) arr[i][j+1] = s[j]-'A'; } for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ ++cntR[arr[i][j]][i]; if(cntR[arr[i][j]][i] == 1) ++diffR[i]; } } for(int i=1;i<=m;++i){ for(int j=1;j<=n;++j){ ++cntC[arr[j][i]][i]; if(cntC[arr[j][i]][i] == 1) ++diffC[i]; } } vector<pair<pair<bool, int>, int>> moves; queue<pair<bool, int>> Q; for(int i=1;i<=n;++i){ if(diffR[i] == 1){ for(int j=0;j<A;++j){ if(cntR[j][i]){ moves.pb({{0, i}, j}); Q.push({0, i}); break; } } } } for(int i=1;i<=m;++i){ if(diffC[i] == 1){ for(int j=0;j<A;++j){ if(cntC[j][i]){ moves.pb({{1, i}, j}); Q.push({1, i}); break; } } } } while(!Q.empty()){ bool col = Q.front().first; int nr = Q.front().second; Q.pop(); if(!col){ for(int i=1;i<=m;++i){ --cntC[arr[nr][i]][i]; if(!cntC[arr[nr][i]][i]){ --diffC[i]; if(diffC[i] == 1){ for(int j=0;j<A;++j){ if(cntC[j][i]){ moves.pb({{1, i}, j}); Q.push({1, i}); break; } } } } } }else{ for(int i=1;i<=n;++i){ --cntR[arr[i][nr]][i]; if(!cntR[arr[i][nr]][i]){ --diffR[i]; if(diffR[i] == 1){ for(int j=0;j<A;++j){ if(cntR[j][i]){ moves.pb({{0, i}, j}); Q.push({0, i}); break; } } } } } } } reverse(moves.begin(), moves.end()); cout<<moves.size()<<"\n"; for(auto &p:moves) cout<<(p.first.first ? "K" : "R")<<" "<<p.first.second<<" "<<char(p.second+'A')<<"\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 | #include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; typedef long long ll; typedef pair<ll,ll> pll; typedef pair<ll,int> pli; typedef pair<int,ll> pil; typedef pair<int,int> pii; const ll INFLL=1e18+7; const int INF=1e9+7; #define pb push_back const int MAXN = 2e3+7, A = 26; int arr[MAXN][MAXN]; int cntR[A][MAXN], cntC[A][MAXN]; int diffR[MAXN], diffC[MAXN]; int main() { ios_base::sync_with_stdio(0); int n,m; cin>>n>>m; for(int i=1;i<=n;++i){ string s; cin>>s; for(int j=0;j<m;++j) arr[i][j+1] = s[j]-'A'; } for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ ++cntR[arr[i][j]][i]; if(cntR[arr[i][j]][i] == 1) ++diffR[i]; } } for(int i=1;i<=m;++i){ for(int j=1;j<=n;++j){ ++cntC[arr[j][i]][i]; if(cntC[arr[j][i]][i] == 1) ++diffC[i]; } } vector<pair<pair<bool, int>, int>> moves; queue<pair<bool, int>> Q; for(int i=1;i<=n;++i){ if(diffR[i] == 1){ for(int j=0;j<A;++j){ if(cntR[j][i]){ moves.pb({{0, i}, j}); Q.push({0, i}); break; } } } } for(int i=1;i<=m;++i){ if(diffC[i] == 1){ for(int j=0;j<A;++j){ if(cntC[j][i]){ moves.pb({{1, i}, j}); Q.push({1, i}); break; } } } } while(!Q.empty()){ bool col = Q.front().first; int nr = Q.front().second; Q.pop(); if(!col){ for(int i=1;i<=m;++i){ --cntC[arr[nr][i]][i]; if(!cntC[arr[nr][i]][i]){ --diffC[i]; if(diffC[i] == 1){ for(int j=0;j<A;++j){ if(cntC[j][i]){ moves.pb({{1, i}, j}); Q.push({1, i}); break; } } } } } }else{ for(int i=1;i<=n;++i){ --cntR[arr[i][nr]][i]; if(!cntR[arr[i][nr]][i]){ --diffR[i]; if(diffR[i] == 1){ for(int j=0;j<A;++j){ if(cntR[j][i]){ moves.pb({{0, i}, j}); Q.push({0, i}); break; } } } } } } } reverse(moves.begin(), moves.end()); cout<<moves.size()<<"\n"; for(auto &p:moves) cout<<(p.first.first ? "K" : "R")<<" "<<p.first.second<<" "<<char(p.second+'A')<<"\n"; } |