#include<bits/stdc++.h> using namespace std; typedef long long ll; #define ssize(a) ((int)(a.size())) int alf = 'Z' - 'A' + 1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<vector<char>>pl(n, vector<char>(m)); vector<vector<int>>wier(n, vector<int>(alf)), kol(m, vector<int>(alf)); vector<int>nzwier(n), nzkol(m); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { cin >> pl[i][j]; ++kol[j][pl[i][j] - 'A']; ++wier[i][pl[i][j] - 'A']; } } queue<pair<pair<char, int>, char>>q; for(int i = 0; i < n; ++i) { char xd = 0; for(int j = 0; j < alf; ++j) if(wier[i][j] != 0) { ++nzwier[i]; xd = char(j) + 'A'; } if(nzwier[i] == 1) q.push({{'R', i}, xd}); } for(int i = 0; i < m; ++i) { char xd = 0; for(int j = 0; j < alf; ++j) if(kol[i][j] != 0) { ++nzkol[i]; xd = char(j) + 'A'; } if(nzkol[i] == 1) q.push({{'K', i}, xd}); } vector<pair<pair<char, int>, char>>odp; while(!q.empty()) { pair<char, int>t = q.front().first; odp.push_back(q.front()); q.pop(); if(t.first == 'R') { for(int i = 0; i < m; ++i) { if(pl[t.second][i] != '.') { --kol[i][pl[t.second][i] - 'A']; if(kol[i][pl[t.second][i] - 'A'] == 0) { --nzkol[i]; if(nzkol[i] == 1) for(int j = 0; j < alf; ++j) if(kol[i][j] != 0) q.push({{'K', i}, char(j + 'A')}); } pl[t.second][i] = '.'; } } } else { for(int i = 0; i < n; ++i) { if(pl[i][t.second] != '.') { --wier[i][pl[i][t.second] - 'A']; if(wier[i][pl[i][t.second] - 'A'] == 0) { --nzwier[i]; if(nzwier[i] == 1) for(int j = 0; j < alf; ++j) if(wier[i][j] != 0) q.push({{'R', i}, char(j + 'A')}); } pl[i][t.second] = '.'; } } } } reverse(odp.begin(), odp.end()); cout<<odp.size()<<endl; for(auto i: odp) cout<<i.first.first<<' '<<i.first.second + 1<<" "<<i.second<<endl; return 0; }
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; #define ssize(a) ((int)(a.size())) int alf = 'Z' - 'A' + 1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<vector<char>>pl(n, vector<char>(m)); vector<vector<int>>wier(n, vector<int>(alf)), kol(m, vector<int>(alf)); vector<int>nzwier(n), nzkol(m); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { cin >> pl[i][j]; ++kol[j][pl[i][j] - 'A']; ++wier[i][pl[i][j] - 'A']; } } queue<pair<pair<char, int>, char>>q; for(int i = 0; i < n; ++i) { char xd = 0; for(int j = 0; j < alf; ++j) if(wier[i][j] != 0) { ++nzwier[i]; xd = char(j) + 'A'; } if(nzwier[i] == 1) q.push({{'R', i}, xd}); } for(int i = 0; i < m; ++i) { char xd = 0; for(int j = 0; j < alf; ++j) if(kol[i][j] != 0) { ++nzkol[i]; xd = char(j) + 'A'; } if(nzkol[i] == 1) q.push({{'K', i}, xd}); } vector<pair<pair<char, int>, char>>odp; while(!q.empty()) { pair<char, int>t = q.front().first; odp.push_back(q.front()); q.pop(); if(t.first == 'R') { for(int i = 0; i < m; ++i) { if(pl[t.second][i] != '.') { --kol[i][pl[t.second][i] - 'A']; if(kol[i][pl[t.second][i] - 'A'] == 0) { --nzkol[i]; if(nzkol[i] == 1) for(int j = 0; j < alf; ++j) if(kol[i][j] != 0) q.push({{'K', i}, char(j + 'A')}); } pl[t.second][i] = '.'; } } } else { for(int i = 0; i < n; ++i) { if(pl[i][t.second] != '.') { --wier[i][pl[i][t.second] - 'A']; if(wier[i][pl[i][t.second] - 'A'] == 0) { --nzwier[i]; if(nzwier[i] == 1) for(int j = 0; j < alf; ++j) if(wier[i][j] != 0) q.push({{'R', i}, char(j + 'A')}); } pl[i][t.second] = '.'; } } } } reverse(odp.begin(), odp.end()); cout<<odp.size()<<endl; for(auto i: odp) cout<<i.first.first<<' '<<i.first.second + 1<<" "<<i.second<<endl; return 0; } |