#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v){ for(const auto &u: v){ os << u << ' '; } return os; } template<typename T> std::istream& operator>>(std::istream& is, std::vector<T>& v){ for(auto& u : v){ is >> u; } return is; } #define fi first #define se second #define pb push_back #define all(a) a.begin(), a.end() #define endl '\n' #define alf 'z' + 1 #define io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define yn (solve() ? "YES" : "NO") typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef tree<int,null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset; //erase: oms.erase(oms.upper_bound(value)) const int mod = 1e9 + 7; const int inf = 1e9 + 7; const int mak = 2e5 + 7; const int dif = 'Z' - 'A' + 2; struct ANS{ char axis; int numb; char color; }; vector<vector<int>> x, y; vector<vector<char>> mat; vector<ANS> ans; inline int nu(char c){ return c - 'A'; } inline int ile(vector<int> v){ int cnt = 0; for(auto u: v) if(u != 0) cnt++; return cnt; } inline char kolor(vector<int> v){ char kolor; for(int i = 0; i < v.size(); i++) if(v[i] != 0) kolor = 'A' + i; return kolor; } int main(){ io; int n, m; cin >> n >> m; x.resize(n + 1); y.resize(m + 1); mat.resize(n + 1); for(int i = 1; i <= n; i++) x[i].resize(dif); for(int j = 1; j <= m; j++) y[j].resize(dif); for(int i = 1; i <= n; i++){ mat[i].resize(m + 1); for(int j = 1; j <= m; j++){ cin >> mat[i][j]; int c = nu(mat[i][j]); x[i][c]++; y[j][c]++; } } bool changed = true; while(changed){ changed = false; for(int i = 1; i <= n; i++){ if(ile(x[i]) == 1){ char c = kolor(x[i]); int cint = c - 'A'; ans.pb({'R', i, c}); x[i][cint] = 0; for(int j = 1; j <= m; j++){ if(mat[i][j] == c){ y[j][cint]--; mat[i][j] = -1; } } changed = true; break; } } if(changed) continue; for(int j = 1; j <= m; j++){ if(ile(y[j]) == 1){ char c = kolor(y[j]); int cint = c - 'A'; ans.pb({'K', j, c}); y[j][cint] = 0; for(int i = 1; i <= n; i++){ if(mat[i][j] == c){ x[i][cint]--; mat[i][j] = -1; } } changed = true; break; } } } int s = ans.size(); cout << s << endl; for(int i = s - 1; i >= 0; i--) cout << ans[i].axis << ' ' << ans[i].numb << ' ' << ans[i].color << 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 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v){ for(const auto &u: v){ os << u << ' '; } return os; } template<typename T> std::istream& operator>>(std::istream& is, std::vector<T>& v){ for(auto& u : v){ is >> u; } return is; } #define fi first #define se second #define pb push_back #define all(a) a.begin(), a.end() #define endl '\n' #define alf 'z' + 1 #define io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define yn (solve() ? "YES" : "NO") typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef tree<int,null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset; //erase: oms.erase(oms.upper_bound(value)) const int mod = 1e9 + 7; const int inf = 1e9 + 7; const int mak = 2e5 + 7; const int dif = 'Z' - 'A' + 2; struct ANS{ char axis; int numb; char color; }; vector<vector<int>> x, y; vector<vector<char>> mat; vector<ANS> ans; inline int nu(char c){ return c - 'A'; } inline int ile(vector<int> v){ int cnt = 0; for(auto u: v) if(u != 0) cnt++; return cnt; } inline char kolor(vector<int> v){ char kolor; for(int i = 0; i < v.size(); i++) if(v[i] != 0) kolor = 'A' + i; return kolor; } int main(){ io; int n, m; cin >> n >> m; x.resize(n + 1); y.resize(m + 1); mat.resize(n + 1); for(int i = 1; i <= n; i++) x[i].resize(dif); for(int j = 1; j <= m; j++) y[j].resize(dif); for(int i = 1; i <= n; i++){ mat[i].resize(m + 1); for(int j = 1; j <= m; j++){ cin >> mat[i][j]; int c = nu(mat[i][j]); x[i][c]++; y[j][c]++; } } bool changed = true; while(changed){ changed = false; for(int i = 1; i <= n; i++){ if(ile(x[i]) == 1){ char c = kolor(x[i]); int cint = c - 'A'; ans.pb({'R', i, c}); x[i][cint] = 0; for(int j = 1; j <= m; j++){ if(mat[i][j] == c){ y[j][cint]--; mat[i][j] = -1; } } changed = true; break; } } if(changed) continue; for(int j = 1; j <= m; j++){ if(ile(y[j]) == 1){ char c = kolor(y[j]); int cint = c - 'A'; ans.pb({'K', j, c}); y[j][cint] = 0; for(int i = 1; i <= n; i++){ if(mat[i][j] == c){ x[i][cint]--; mat[i][j] = -1; } } changed = true; break; } } } int s = ans.size(); cout << s << endl; for(int i = s - 1; i >= 0; i--) cout << ans[i].axis << ' ' << ans[i].numb << ' ' << ans[i].color << endl; } |