#include <bits/stdc++.h> using namespace std; typedef long long int ll; constexpr int N = 2000; string mapa[N+9]; int cntr[N+9][30]; int cntk[N+9][30]; bool usedr[N+9]; bool usedk[N+9]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin >> n >> m; for (int x=0;x<n;x++)cin>>mapa[x]; for (int x=0;x<n;x++){ for (int y=0;y<m;y++){ cntr[x][mapa[x][y]-'A']++; cntk[y][mapa[x][y]-'A']++; } } vector<pair<char,pair<int,char>>> ruchy; int cnt=0; while(cnt<n+m){ for (int x=0;x<n;x++){ int ile=0; if (usedr[x])continue; for (int a=0;a<27;a++){ if (cntr[x][a]>0)ile++; } if (ile==0){ usedr[x]=1; cnt++; continue; } if (ile==1){ for (int a=0;a<27;a++){ if (cntr[x][a]>0){ ruchy.push_back({'R',{x+1,'A'+a}}); break; } } cnt++; usedr[x]=1; for (int y=0;y<m;y++){ cntk[y][mapa[x][y]-'A']--; } } } for (int y=0;y<m;y++){ int ile=0; if (usedk[y])continue; for (int a=0;a<27;a++){ if (cntk[y][a]>0)ile++; } if (ile==0){ cnt++; usedk[y]=1; continue; } if (ile==1){ for (int a=0;a<27;a++){ if (cntk[y][a]>0){ ruchy.push_back({'K',{y+1,'A'+a}}); break; } } cnt++; usedk[y]=1; for (int x=0;x<n;x++){ cntr[x][mapa[x][y]-'A']--; } } } } cout << ruchy.size() << '\n'; reverse(ruchy.begin(),ruchy.end()); for (pair<char,pair<int,char>> xd:ruchy){ cout << xd.first << ' ' << xd.second.first << ' ' << xd.second.second << '\n'; } 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 | #include <bits/stdc++.h> using namespace std; typedef long long int ll; constexpr int N = 2000; string mapa[N+9]; int cntr[N+9][30]; int cntk[N+9][30]; bool usedr[N+9]; bool usedk[N+9]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin >> n >> m; for (int x=0;x<n;x++)cin>>mapa[x]; for (int x=0;x<n;x++){ for (int y=0;y<m;y++){ cntr[x][mapa[x][y]-'A']++; cntk[y][mapa[x][y]-'A']++; } } vector<pair<char,pair<int,char>>> ruchy; int cnt=0; while(cnt<n+m){ for (int x=0;x<n;x++){ int ile=0; if (usedr[x])continue; for (int a=0;a<27;a++){ if (cntr[x][a]>0)ile++; } if (ile==0){ usedr[x]=1; cnt++; continue; } if (ile==1){ for (int a=0;a<27;a++){ if (cntr[x][a]>0){ ruchy.push_back({'R',{x+1,'A'+a}}); break; } } cnt++; usedr[x]=1; for (int y=0;y<m;y++){ cntk[y][mapa[x][y]-'A']--; } } } for (int y=0;y<m;y++){ int ile=0; if (usedk[y])continue; for (int a=0;a<27;a++){ if (cntk[y][a]>0)ile++; } if (ile==0){ cnt++; usedk[y]=1; continue; } if (ile==1){ for (int a=0;a<27;a++){ if (cntk[y][a]>0){ ruchy.push_back({'K',{y+1,'A'+a}}); break; } } cnt++; usedk[y]=1; for (int x=0;x<n;x++){ cntr[x][mapa[x][y]-'A']--; } } } } cout << ruchy.size() << '\n'; reverse(ruchy.begin(),ruchy.end()); for (pair<char,pair<int,char>> xd:ruchy){ cout << xd.first << ' ' << xd.second.first << ' ' << xd.second.second << '\n'; } return 0; } |