#include <bits/stdc++.h> using namespace std; struct W{ char z; int t,l; W(int a,int b,char c){ t = a; l = b; z = c; } }; stack <W> odp; ostream& operator<<(ostream& os,W& a){ if(a.t) os << "K" << " "; else os << "R" << " "; os << a.l << " " << (char)(a.z); return os; } const int N = 2007; char tab[N][N]; int n,m; unordered_map <int,int> r[N],k[N]; unordered_set <int> sr[N],sk[N]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ cin >> tab[i][j]; } } for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ r[i][tab[i][j]]++; sr[i].insert(tab[i][j]); } } for(int i = 1;i <= m;i++){ for(int j = 1;j <= n;j++){ k[i][tab[j][i]]++; sk[i].insert(tab[j][i]); } } queue <W> q; // for(int i = 1;i <= n;i++){ // cerr << i << "\n"; // for(auto t : sr[i]) cerr << t << " "; // cerr << "\n"; // for(auto t : r[i]) cerr << t.first << " " << t.second << " : "; // cerr << "\n----------------------\n"; // } // for(int i = 1;i <= m;i++){ // cerr << i << "\n"; // for(auto t : sk[i]) cerr << t << " "; // cerr << "\n"; // for(auto t : k[i]) cerr << t.first << " " << t.second << " : "; // cerr << "\n----------------------\n"; // } for(int i = 1;i <= n;i++) if(sr[i].size() == 1) q.push(W(0,i,*(sr[i].begin()))); for(int i = 1;i <= m;i++) if(sk[i].size() == 1) q.push(W(1,i,*(sk[i].begin()))); while(!q.empty()){ W t = q.front(); odp.push(t); q.pop(); if(t.t){ for(int i = 1;i <= n;i++) if(!sr[i].empty()){ r[i][t.z]--; if(r[i][t.z] == 0){ sr[i].erase(t.z); if(sr[i].size() == 1){ q.push(W(0,i,*(sr[i].begin()))); sr[i].clear(); } } } }else{ for(int i = 1;i <= m;i++) if(!sk[i].empty()){ k[i][t.z]--; if(k[i][t.z] == 0){ sk[i].erase(t.z); if(sk[i].size() == 1){ q.push(W(1,i,*(sk[i].begin()))); sk[i].clear(); } } } } } cout << odp.size() << "\n"; while(!odp.empty()){ cout << odp.top() << "\n"; odp.pop(); } 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 | #include <bits/stdc++.h> using namespace std; struct W{ char z; int t,l; W(int a,int b,char c){ t = a; l = b; z = c; } }; stack <W> odp; ostream& operator<<(ostream& os,W& a){ if(a.t) os << "K" << " "; else os << "R" << " "; os << a.l << " " << (char)(a.z); return os; } const int N = 2007; char tab[N][N]; int n,m; unordered_map <int,int> r[N],k[N]; unordered_set <int> sr[N],sk[N]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ cin >> tab[i][j]; } } for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ r[i][tab[i][j]]++; sr[i].insert(tab[i][j]); } } for(int i = 1;i <= m;i++){ for(int j = 1;j <= n;j++){ k[i][tab[j][i]]++; sk[i].insert(tab[j][i]); } } queue <W> q; // for(int i = 1;i <= n;i++){ // cerr << i << "\n"; // for(auto t : sr[i]) cerr << t << " "; // cerr << "\n"; // for(auto t : r[i]) cerr << t.first << " " << t.second << " : "; // cerr << "\n----------------------\n"; // } // for(int i = 1;i <= m;i++){ // cerr << i << "\n"; // for(auto t : sk[i]) cerr << t << " "; // cerr << "\n"; // for(auto t : k[i]) cerr << t.first << " " << t.second << " : "; // cerr << "\n----------------------\n"; // } for(int i = 1;i <= n;i++) if(sr[i].size() == 1) q.push(W(0,i,*(sr[i].begin()))); for(int i = 1;i <= m;i++) if(sk[i].size() == 1) q.push(W(1,i,*(sk[i].begin()))); while(!q.empty()){ W t = q.front(); odp.push(t); q.pop(); if(t.t){ for(int i = 1;i <= n;i++) if(!sr[i].empty()){ r[i][t.z]--; if(r[i][t.z] == 0){ sr[i].erase(t.z); if(sr[i].size() == 1){ q.push(W(0,i,*(sr[i].begin()))); sr[i].clear(); } } } }else{ for(int i = 1;i <= m;i++) if(!sk[i].empty()){ k[i][t.z]--; if(k[i][t.z] == 0){ sk[i].erase(t.z); if(sk[i].size() == 1){ q.push(W(1,i,*(sk[i].begin()))); sk[i].clear(); } } } } } cout << odp.size() << "\n"; while(!odp.empty()){ cout << odp.top() << "\n"; odp.pop(); } return 0; } |