#include <bits/stdc++.h> #define pb push_back using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; const int N = 2003; char tab[N][N]; int kolor_w[N][30]; int kolor_k[N][30]; vector<tuple<char, int, char>> seg; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { char c; cin >> c; tab[i][j] = c; kolor_w[i][c-'A']++; kolor_k[j][c-'A']++; } } int all = 0; for(int t = 1; t <= n+m; t++){ for(int i = 1; i <= n; i++) { // wiersze int count = 0; char c; for(int k = 0; k < 26; k++) { if(kolor_w[i][k] > 0) { c = 'A' + k; count++; } } if(count == 1) { for(int kol = 0; kol < 26; kol++) kolor_w[i][kol] = 0; seg.pb({'R', i, c}); for(int j = 1; j <= m; j++) { kolor_k[j][tab[i][j]-'A']--; } all++; break; } } for(int j = 1; j <= m; j++) { // kolumny int count = 0; char c; for(int k = 0; k < 26; k++) { if(kolor_k[j][k] > 0) { c = 'A' + k; count++; } } if(count == 1) { for(int wie = 0; wie < 26; wie++) kolor_k[j][wie] = 0; seg.pb({'K', j, c}); for(int i = 1; i <= n; i++) { kolor_w[i][tab[i][j]-'A']--; } all++; break; } } } reverse(seg.begin(), seg.end()); cout << all << "\n"; for(auto x : seg) cout << get<0>(x) << " " << get<1>(x) << " " << get<2>(x) << "\n"; 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 | #include <bits/stdc++.h> #define pb push_back using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; const int N = 2003; char tab[N][N]; int kolor_w[N][30]; int kolor_k[N][30]; vector<tuple<char, int, char>> seg; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { char c; cin >> c; tab[i][j] = c; kolor_w[i][c-'A']++; kolor_k[j][c-'A']++; } } int all = 0; for(int t = 1; t <= n+m; t++){ for(int i = 1; i <= n; i++) { // wiersze int count = 0; char c; for(int k = 0; k < 26; k++) { if(kolor_w[i][k] > 0) { c = 'A' + k; count++; } } if(count == 1) { for(int kol = 0; kol < 26; kol++) kolor_w[i][kol] = 0; seg.pb({'R', i, c}); for(int j = 1; j <= m; j++) { kolor_k[j][tab[i][j]-'A']--; } all++; break; } } for(int j = 1; j <= m; j++) { // kolumny int count = 0; char c; for(int k = 0; k < 26; k++) { if(kolor_k[j][k] > 0) { c = 'A' + k; count++; } } if(count == 1) { for(int wie = 0; wie < 26; wie++) kolor_k[j][wie] = 0; seg.pb({'K', j, c}); for(int i = 1; i <= n; i++) { kolor_w[i][tab[i][j]-'A']--; } all++; break; } } } reverse(seg.begin(), seg.end()); cout << all << "\n"; for(auto x : seg) cout << get<0>(x) << " " << get<1>(x) << " " << get<2>(x) << "\n"; return 0; } |