#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<char, pii> pp; const int N = 2e3+10; const int C = 27; int n, m, cc[N][C+10], cr[N][C+10]; char b[N][N]; bool rm[N][N]; bool vc[N+10], vr[N+10]; pii cntr(int i) { int x=0, r=-1; for (int j=0; j<C; ++j) { if (cr[i][j]) ++x, r=j; } if (x == 1) return {x, r}; return {-1, -1}; } pii cntc(int j) { int x=0, r=-1; for (int i=0; i<C; ++i) { if (cc[j][i]) ++x, r=i; } if (x == 1) return {x, r}; return {-1, -1}; } char bc[N+1][N+1]; bool sim(vector<pp> mv) { for (auto u : mv) { if (u.first == 'K') { for (int i=1; i<=n; ++i) { bc[i][u.second.first]=char('A'+u.second.second); } } else { for (int j=1; j<=m; ++j) { bc[u.second.first][j]=char('A'+u.second.second); } } } for (int i=1; i<=n; ++i) { for (int j=1; j<=m; ++j) assert(b[i][j] == bc[i][j]); } return true; } void solve() { cin>>n>>m; for (int i=1; i<=n; ++i) { string s; cin>>s; for (int j=0; j<m; ++j) b[i][j+1]=s[j]; } queue<pp> q; for (int i=1; i<=n; ++i) { for (int j=1; j<=m; ++j) ++cr[i][b[i][j]-'A']; pii x=cntr(i); if (x.first == 1) q.push({'R', {i, x.second}}), vr[i]=true; } for (int j=1; j<=m; ++j) { for (int i=1; i<=n; ++i) ++cc[j][b[i][j]-'A']; pii x=cntc(j); if (x.first == 1) q.push({'K', {j, x.second}}), vc[j]=true; } vector<pp> mv; while (!q.empty()) { pp x=q.front(); q.pop(); mv.push_back(x); if (x.first == 'R') { // row int i=x.second.first; for (int j=1; j<=m; ++j) { if (rm[i][j]) continue; rm[i][j]=true; --cc[j][b[i][j]-'A']; pii x=cntc(j); if (x.first == 1 && !vc[j]) { q.push({'K', {j, x.second}}); vc[j]=true; } } } else { // column int j=x.second.first; for (int i=1; i<=n; ++i) { if (rm[i][j]) continue; rm[i][j]=true; --cr[i][b[i][j]-'A']; pii x=cntr(i); if (x.first == 1 && !vr[i]) { q.push({'R', {i, x.second}}); vr[i]=true; } } } } reverse(mv.begin(), mv.end()); assert((int)mv.size() <= n+m); assert(sim(mv)); cout<<mv.size()<<"\n"; for (auto u : mv) { cout<<u.first<<" "<<u.second.first<<" "<<char(u.second.second+'A')<<"\n"; } } int main() { //ios_base::sync_with_stdio(0); //cin.tie(NULL); //cout.tie(NULL); int t=1; //cin>>t; while (t--) solve(); }
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<char, pii> pp; const int N = 2e3+10; const int C = 27; int n, m, cc[N][C+10], cr[N][C+10]; char b[N][N]; bool rm[N][N]; bool vc[N+10], vr[N+10]; pii cntr(int i) { int x=0, r=-1; for (int j=0; j<C; ++j) { if (cr[i][j]) ++x, r=j; } if (x == 1) return {x, r}; return {-1, -1}; } pii cntc(int j) { int x=0, r=-1; for (int i=0; i<C; ++i) { if (cc[j][i]) ++x, r=i; } if (x == 1) return {x, r}; return {-1, -1}; } char bc[N+1][N+1]; bool sim(vector<pp> mv) { for (auto u : mv) { if (u.first == 'K') { for (int i=1; i<=n; ++i) { bc[i][u.second.first]=char('A'+u.second.second); } } else { for (int j=1; j<=m; ++j) { bc[u.second.first][j]=char('A'+u.second.second); } } } for (int i=1; i<=n; ++i) { for (int j=1; j<=m; ++j) assert(b[i][j] == bc[i][j]); } return true; } void solve() { cin>>n>>m; for (int i=1; i<=n; ++i) { string s; cin>>s; for (int j=0; j<m; ++j) b[i][j+1]=s[j]; } queue<pp> q; for (int i=1; i<=n; ++i) { for (int j=1; j<=m; ++j) ++cr[i][b[i][j]-'A']; pii x=cntr(i); if (x.first == 1) q.push({'R', {i, x.second}}), vr[i]=true; } for (int j=1; j<=m; ++j) { for (int i=1; i<=n; ++i) ++cc[j][b[i][j]-'A']; pii x=cntc(j); if (x.first == 1) q.push({'K', {j, x.second}}), vc[j]=true; } vector<pp> mv; while (!q.empty()) { pp x=q.front(); q.pop(); mv.push_back(x); if (x.first == 'R') { // row int i=x.second.first; for (int j=1; j<=m; ++j) { if (rm[i][j]) continue; rm[i][j]=true; --cc[j][b[i][j]-'A']; pii x=cntc(j); if (x.first == 1 && !vc[j]) { q.push({'K', {j, x.second}}); vc[j]=true; } } } else { // column int j=x.second.first; for (int i=1; i<=n; ++i) { if (rm[i][j]) continue; rm[i][j]=true; --cr[i][b[i][j]-'A']; pii x=cntr(i); if (x.first == 1 && !vr[i]) { q.push({'R', {i, x.second}}); vr[i]=true; } } } } reverse(mv.begin(), mv.end()); assert((int)mv.size() <= n+m); assert(sim(mv)); cout<<mv.size()<<"\n"; for (auto u : mv) { cout<<u.first<<" "<<u.second.first<<" "<<char(u.second.second+'A')<<"\n"; } } int main() { //ios_base::sync_with_stdio(0); //cin.tie(NULL); //cout.tie(NULL); int t=1; //cin>>t; while (t--) solve(); } |