#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<(int)b;++i) #define FORD(i,a,b) for(int i=a;i>=(int)b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } template<class T, class U> ostream& operator<< (ostream& os, const pair<T, U>& p){ os << p.FI << " " << p.SE; return os; } template<class T, class U> istream& operator>> (istream& is, pair<T, U>& p){ is >> p.FI >> p.SE; return is; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<string> V(n); cin >> V; vector<map<char, int>> R(n); vector<map<char, int>> C(m); FOR(i,0,n) FOR(j,0,m) { R[i][V[i][j]]++; C[j][V[i][j]]++; } queue<pii> Q; FOR(i,0,n) if (R[i].size() == 1) Q.emplace(0, i); FOR(i,0,m) if (C[i].size() == 1) Q.emplace(1, i); vector<pair<char, pair<int, char>>> sol; while(!Q.empty()){ pii t = Q.front(); Q.pop(); if (t.FI == 0){ if(R[t.SE].size() != 1) continue; char col = R[t.SE].begin() -> FI; sol.PB({'R', {t.SE + 1, col}}); FOR(j,0,m) { if (V[t.SE][j] != 0){ V[t.SE][j] = 0; if (C[j][col] > 1) C[j][col]--; else{ C[j].erase(col); if (C[j].size() == 1) Q.emplace(1, j); } } } R[t.SE].clear(); } else { if(C[t.SE].size() != 1) continue; char col = C[t.SE].begin() -> FI; sol.PB({'K', {t.SE + 1, col}}); FOR(i,0,n) { if (V[i][t.SE] != 0){ V[i][t.SE] = 0; if (R[i][col] > 1) R[i][col]--; else{ R[i].erase(col); if (R[i].size() == 1) Q.emplace(0, i); } } } C[t.SE].clear(); } } reverse(ALL(sol)); cout << sol.size() << endl; for(auto& p : sol) cout << p << 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 98 99 100 101 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<(int)b;++i) #define FORD(i,a,b) for(int i=a;i>=(int)b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } template<class T, class U> ostream& operator<< (ostream& os, const pair<T, U>& p){ os << p.FI << " " << p.SE; return os; } template<class T, class U> istream& operator>> (istream& is, pair<T, U>& p){ is >> p.FI >> p.SE; return is; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<string> V(n); cin >> V; vector<map<char, int>> R(n); vector<map<char, int>> C(m); FOR(i,0,n) FOR(j,0,m) { R[i][V[i][j]]++; C[j][V[i][j]]++; } queue<pii> Q; FOR(i,0,n) if (R[i].size() == 1) Q.emplace(0, i); FOR(i,0,m) if (C[i].size() == 1) Q.emplace(1, i); vector<pair<char, pair<int, char>>> sol; while(!Q.empty()){ pii t = Q.front(); Q.pop(); if (t.FI == 0){ if(R[t.SE].size() != 1) continue; char col = R[t.SE].begin() -> FI; sol.PB({'R', {t.SE + 1, col}}); FOR(j,0,m) { if (V[t.SE][j] != 0){ V[t.SE][j] = 0; if (C[j][col] > 1) C[j][col]--; else{ C[j].erase(col); if (C[j].size() == 1) Q.emplace(1, j); } } } R[t.SE].clear(); } else { if(C[t.SE].size() != 1) continue; char col = C[t.SE].begin() -> FI; sol.PB({'K', {t.SE + 1, col}}); FOR(i,0,n) { if (V[i][t.SE] != 0){ V[i][t.SE] = 0; if (R[i][col] > 1) R[i][col]--; else{ R[i].erase(col); if (R[i].size() == 1) Q.emplace(0, i); } } } C[t.SE].clear(); } } reverse(ALL(sol)); cout << sol.size() << endl; for(auto& p : sol) cout << p << endl; } |