#include <bits/stdc++.h> using namespace std; int n,m; //char tab[2020][2020]; int r[2020][30]; int k[2020][30]; int nr[2020]; int nk[2020]; bool odw_r[2020]; bool odw_k[2020]; queue <pair<int, int>> q; stack <pair<char, pair<int, char>>> stos; char find_sign(int typ, int num){ if(typ == 1){ for(int i=0;i<26;i++){ if(r[num][i] > 0){ return (char)(i+'A'); } } } else { for(int i=0;i<26;i++){ if(k[num][i] > 0){ return (char)(i+'A'); } } } return 'X'; } int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ string s; cin>>s; for(int j=1;j<=m;j++){ //tab[i][j] = s[j]; if(r[i][s[j-1] - 'A'] == 0) nr[i]++; r[i][s[j-1] - 'A']++; if(k[j][s[j-1] - 'A'] == 0) nk[j]++; k[j][s[j-1] - 'A']++; } } for(int i=1;i<=n;i++){ if(nr[i] == 1){ q.push({1, i}); odw_r[i] = true; } } for(int j=1;j<=m;j++){ if(nk[j] == 1){ q.push({0, j}); odw_k[j] = true; } } while(!q.empty()){ pair<int, int> p = q.front(); q.pop(); char c = find_sign(p.first, p.second); char x; int kol = c - 'A'; if(p.first == 1){ x = 'R'; for(int j=1;j<=m;j++){ k[j][kol]--; if(k[j][kol] == 0) nk[j] --; if(nk[j] == 1 && odw_k[j] == 0){ q.push({0, j}); odw_k[j] = 1; } } } else { x = 'K'; for(int i=1;i<=n;i++){ r[i][kol]--; if(r[i][kol] == 0) nr[i] --; if(nr[i] == 1 && odw_r[i] == 0){ q.push({1, i}); odw_r[i] = 1; } } } stos.push({x,{p.second, c}}); } cout<<stos.size()<<endl; while(!stos.empty()){ pair<char, pair<int, char>> p = stos.top(); stos.pop(); cout<<p.first<<" "<<p.second.first<<" "<<p.second.second<<endl; } }
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 | #include <bits/stdc++.h> using namespace std; int n,m; //char tab[2020][2020]; int r[2020][30]; int k[2020][30]; int nr[2020]; int nk[2020]; bool odw_r[2020]; bool odw_k[2020]; queue <pair<int, int>> q; stack <pair<char, pair<int, char>>> stos; char find_sign(int typ, int num){ if(typ == 1){ for(int i=0;i<26;i++){ if(r[num][i] > 0){ return (char)(i+'A'); } } } else { for(int i=0;i<26;i++){ if(k[num][i] > 0){ return (char)(i+'A'); } } } return 'X'; } int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ string s; cin>>s; for(int j=1;j<=m;j++){ //tab[i][j] = s[j]; if(r[i][s[j-1] - 'A'] == 0) nr[i]++; r[i][s[j-1] - 'A']++; if(k[j][s[j-1] - 'A'] == 0) nk[j]++; k[j][s[j-1] - 'A']++; } } for(int i=1;i<=n;i++){ if(nr[i] == 1){ q.push({1, i}); odw_r[i] = true; } } for(int j=1;j<=m;j++){ if(nk[j] == 1){ q.push({0, j}); odw_k[j] = true; } } while(!q.empty()){ pair<int, int> p = q.front(); q.pop(); char c = find_sign(p.first, p.second); char x; int kol = c - 'A'; if(p.first == 1){ x = 'R'; for(int j=1;j<=m;j++){ k[j][kol]--; if(k[j][kol] == 0) nk[j] --; if(nk[j] == 1 && odw_k[j] == 0){ q.push({0, j}); odw_k[j] = 1; } } } else { x = 'K'; for(int i=1;i<=n;i++){ r[i][kol]--; if(r[i][kol] == 0) nr[i] --; if(nr[i] == 1 && odw_r[i] == 0){ q.push({1, i}); odw_r[i] = 1; } } } stos.push({x,{p.second, c}}); } cout<<stos.size()<<endl; while(!stos.empty()){ pair<char, pair<int, char>> p = stos.top(); stos.pop(); cout<<p.first<<" "<<p.second.first<<" "<<p.second.second<<endl; } } |