#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define int long long #define st first #define nd second #define pb push_back #define mp make_pair #define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() const int mxN = 2e3+7; char grid[mxN][mxN]; int cntk[mxN], cntw[mxN]; int kol[mxN][30]; int wie[mxN][30]; bool zk[mxN], zw[mxN]; queue<pair<int,char>> qk, qw; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin >> n >> m; rep(i,1,n) rep(j,1,m) cin >> grid[i][j]; rep(i,1,n) rep(j,1,m){ wie[i][grid[i][j]-'A']++; kol[j][grid[i][j]-'A']++; } rep(i,1,n) cntw[i] = m; rep(j,1,m) cntk[j] = n; rep(i,1,n) rep(c,0,25){ if(cntw[i] == wie[i][c]){ qw.push({i, c+'A'}); zw[i] = 1; //cout << "wiersz " << i << " " << char(c+'A') << "\n"; } } rep(j,1,m) rep(c,0,25){ if(cntk[j] == kol[j][c]){ qk.push({j,c+'A'}); zk[j] = 1; //cout << "kolumna " << j << " " << char(c+'A') << "\n"; } } vector<pair<char,pair<int,char>>> res; while(sz(qw) || sz(qk)){ if(sz(qw)){ auto [i,cc] = qw.front(); qw.pop(); res.pb({'R', {i,cc}}); rep(j,1,m){ if(grid[i][j] != '#'){ kol[j][grid[i][j]-'A']--; cntk[j]--; rep(c,0,25) if(cntk[j] == kol[j][c] && !zk[j]){ qk.push({j,c+'A'}); zk[j] = 1; } grid[i][j] = '#'; } } }else{ auto [j,cc] = qk.front(); qk.pop(); res.pb({'K', {j, cc}}); rep(i,1,n){ if(grid[i][j] != '#'){ wie[i][grid[i][j]-'A']--; cntw[i]--; rep(c,0,25) if(cntw[i] == wie[i][c] && !zw[i]){ qw.push({i,c+'A'}); zw[i] = 1; } grid[i][j] = '#'; } } } } reverse(all(res)); cout << sz(res) << "\n"; for(auto u : res) cout << u.st << " " << u.nd.st << " " << u.nd.nd << "\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define int long long #define st first #define nd second #define pb push_back #define mp make_pair #define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() const int mxN = 2e3+7; char grid[mxN][mxN]; int cntk[mxN], cntw[mxN]; int kol[mxN][30]; int wie[mxN][30]; bool zk[mxN], zw[mxN]; queue<pair<int,char>> qk, qw; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin >> n >> m; rep(i,1,n) rep(j,1,m) cin >> grid[i][j]; rep(i,1,n) rep(j,1,m){ wie[i][grid[i][j]-'A']++; kol[j][grid[i][j]-'A']++; } rep(i,1,n) cntw[i] = m; rep(j,1,m) cntk[j] = n; rep(i,1,n) rep(c,0,25){ if(cntw[i] == wie[i][c]){ qw.push({i, c+'A'}); zw[i] = 1; //cout << "wiersz " << i << " " << char(c+'A') << "\n"; } } rep(j,1,m) rep(c,0,25){ if(cntk[j] == kol[j][c]){ qk.push({j,c+'A'}); zk[j] = 1; //cout << "kolumna " << j << " " << char(c+'A') << "\n"; } } vector<pair<char,pair<int,char>>> res; while(sz(qw) || sz(qk)){ if(sz(qw)){ auto [i,cc] = qw.front(); qw.pop(); res.pb({'R', {i,cc}}); rep(j,1,m){ if(grid[i][j] != '#'){ kol[j][grid[i][j]-'A']--; cntk[j]--; rep(c,0,25) if(cntk[j] == kol[j][c] && !zk[j]){ qk.push({j,c+'A'}); zk[j] = 1; } grid[i][j] = '#'; } } }else{ auto [j,cc] = qk.front(); qk.pop(); res.pb({'K', {j, cc}}); rep(i,1,n){ if(grid[i][j] != '#'){ wie[i][grid[i][j]-'A']--; cntw[i]--; rep(c,0,25) if(cntw[i] == wie[i][c] && !zw[i]){ qw.push({i,c+'A'}); zw[i] = 1; } grid[i][j] = '#'; } } } } reverse(all(res)); cout << sz(res) << "\n"; for(auto u : res) cout << u.st << " " << u.nd.st << " " << u.nd.nd << "\n"; } |