#include <bits/stdc++.h> using namespace std; #define st first #define nd second #define ll long long const ll maxn = 2e3 + 7; ll n, m, l, r, x; char ch; ll t[maxn][maxn], w[maxn][50], k[maxn][50]; queue<pair<ll, ll>> q; stack<pair<ll, ll>> s; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cin >> ch; x = int(ch - 'A') + 1; t[i][j] = x; if(w[i][x] == 0) w[i][0]++; w[i][x]++; if(k[j][x] == 0) k[j][0]++; k[j][x]++; } } for(int i = 1; i <= n; i++){ if(w[i][0] == 1){ for(int j = 1; j <= 26; j++){ if(w[i][j] > 0){ q.push({i, j}); s.push({i, j}); break; } } } } for(int i = 1; i <= m; i++){ if(k[i][0] == 1){ for(int j = 1; j <= 26; j++){ if(k[i][j] > 0){ q.push({-i, j}); s.push({-i, j}); break; } } } } while(!q.empty()){ l = q.front().nd; r = q.front().st; // cout << r << " " << l << '\n'; q.pop(); if(r > 0){ for(int i = 1; i <= m; i++){ k[i][l]--; if(k[i][l] == 0){ k[i][0]--; if(k[i][0] == 1){ for(int j = 1; j <= 26; j++){ if(k[i][j] > 0){ q.push({-i, j}); s.push({-i, j}); break; } } } } } } else{ r *= (-1); for(int i = 1; i <= n; i++){ w[i][l]--; if(w[i][l] == 0){ w[i][0]--; if(w[i][0] == 1){ for(int j = 1; j <= 26; j++){ if(w[i][j] > 0){ q.push({i, j}); s.push({i, j}); break; } } } } } } } cout << s.size() << '\n'; while(s.size()){ if(s.top().st < 0){ cout << "K " << -s.top().st << " " << char(s.top().nd + 'A' - 1) << '\n'; } else cout << "R " << s.top().st << " " << char(s.top().nd + 'A' - 1) << '\n'; s.pop(); } }
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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define ll long long const ll maxn = 2e3 + 7; ll n, m, l, r, x; char ch; ll t[maxn][maxn], w[maxn][50], k[maxn][50]; queue<pair<ll, ll>> q; stack<pair<ll, ll>> s; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cin >> ch; x = int(ch - 'A') + 1; t[i][j] = x; if(w[i][x] == 0) w[i][0]++; w[i][x]++; if(k[j][x] == 0) k[j][0]++; k[j][x]++; } } for(int i = 1; i <= n; i++){ if(w[i][0] == 1){ for(int j = 1; j <= 26; j++){ if(w[i][j] > 0){ q.push({i, j}); s.push({i, j}); break; } } } } for(int i = 1; i <= m; i++){ if(k[i][0] == 1){ for(int j = 1; j <= 26; j++){ if(k[i][j] > 0){ q.push({-i, j}); s.push({-i, j}); break; } } } } while(!q.empty()){ l = q.front().nd; r = q.front().st; // cout << r << " " << l << '\n'; q.pop(); if(r > 0){ for(int i = 1; i <= m; i++){ k[i][l]--; if(k[i][l] == 0){ k[i][0]--; if(k[i][0] == 1){ for(int j = 1; j <= 26; j++){ if(k[i][j] > 0){ q.push({-i, j}); s.push({-i, j}); break; } } } } } } else{ r *= (-1); for(int i = 1; i <= n; i++){ w[i][l]--; if(w[i][l] == 0){ w[i][0]--; if(w[i][0] == 1){ for(int j = 1; j <= 26; j++){ if(w[i][j] > 0){ q.push({i, j}); s.push({i, j}); break; } } } } } } } cout << s.size() << '\n'; while(s.size()){ if(s.top().st < 0){ cout << "K " << -s.top().st << " " << char(s.top().nd + 'A' - 1) << '\n'; } else cout << "R " << s.top().st << " " << char(s.top().nd + 'A' - 1) << '\n'; s.pop(); } } |