#include <bits/stdc++.h> using namespace std; const int N = 2000; const int A = 26; int rzad[N][A]; int kolumna[N][A]; string znak[N]; struct ruch { char kol_wiersz, kolor; int nr; ruch(char wiersz, char kol, int idx) { kol_wiersz = wiersz; kolor = kol; nr = idx; } }; void check(int ile[N][A], int drugi[N][A], char typ, vector <ruch> &ruchy, int idx, int n) { //cout << typ << "\n"; char x = '1'; for(int i = 0; i < A; i++) { if(ile[idx][i] > 0) { if(x != '1') return; x = char(i + 'A'); } } if(x == '1') return; //cout << x << " " << idx << " " << typ << "\n"; ruchy.push_back(ruch(typ, x, idx)); ile[idx][x - 'A'] = 0; for(int i = 0; i < n; i++) { int idx1 = typ == 'K' ? i : idx; int idx2 = typ == 'K' ? idx : i; if(znak[idx1][idx2] == x) { znak[idx1][idx2] = ' '; drugi[i][x - 'A']--; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for(int i = 0; i < n; i++) { cin >> znak[i]; for(int j = 0; j < m; j++) { int z = znak[i][j] - 'A'; rzad[i][z]++; kolumna[j][z]++; } } vector <ruch> ruchy; for(int X = 1; X <= n + m; X++) { for(int i = 0; i < n; i++) check(rzad, kolumna, 'R', ruchy, i, m); for(int i = 0; i < m; i++) check(kolumna, rzad, 'K', ruchy, i, n); } cout << ruchy.size() << "\n"; for(int i = ruchy.size() - 1; i >= 0; i--) cout << ruchy[i].kol_wiersz << " " << ruchy[i].nr + 1 << " " << ruchy[i].kolor << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; const int N = 2000; const int A = 26; int rzad[N][A]; int kolumna[N][A]; string znak[N]; struct ruch { char kol_wiersz, kolor; int nr; ruch(char wiersz, char kol, int idx) { kol_wiersz = wiersz; kolor = kol; nr = idx; } }; void check(int ile[N][A], int drugi[N][A], char typ, vector <ruch> &ruchy, int idx, int n) { //cout << typ << "\n"; char x = '1'; for(int i = 0; i < A; i++) { if(ile[idx][i] > 0) { if(x != '1') return; x = char(i + 'A'); } } if(x == '1') return; //cout << x << " " << idx << " " << typ << "\n"; ruchy.push_back(ruch(typ, x, idx)); ile[idx][x - 'A'] = 0; for(int i = 0; i < n; i++) { int idx1 = typ == 'K' ? i : idx; int idx2 = typ == 'K' ? idx : i; if(znak[idx1][idx2] == x) { znak[idx1][idx2] = ' '; drugi[i][x - 'A']--; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for(int i = 0; i < n; i++) { cin >> znak[i]; for(int j = 0; j < m; j++) { int z = znak[i][j] - 'A'; rzad[i][z]++; kolumna[j][z]++; } } vector <ruch> ruchy; for(int X = 1; X <= n + m; X++) { for(int i = 0; i < n; i++) check(rzad, kolumna, 'R', ruchy, i, m); for(int i = 0; i < m; i++) check(kolumna, rzad, 'K', ruchy, i, n); } cout << ruchy.size() << "\n"; for(int i = ruchy.size() - 1; i >= 0; i--) cout << ruchy[i].kol_wiersz << " " << ruchy[i].nr + 1 << " " << ruchy[i].kolor << "\n"; } |