#include <bits/stdc++.h> #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long; using ld = long double; //#define int ll #define sz(x) ((int)(x).size()) using pii = pair<int,int>; using tii = tuple<int,int,int>; const int nmax = 2e3 + 5; char mat[nmax][nmax]; struct Map { unordered_map<char,int> a; void emplace(char x) { a[x]++; } void erase(int x) { a[x]--; if(a[x] == 0) a.erase(x); return; } int size() { return sz(a); } }; Map rows[nmax], cols[nmax]; int occr[nmax], occc[nmax]; signed main() { cin.tie(0) -> sync_with_stdio(0); int n, m; cin >> n >> m; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { cin >> mat[i][j]; rows[i].emplace(mat[i][j]); cols[j].emplace(mat[i][j]); } } queue<pair<char, int>> opers; for(int i = 1; i <= n; i++) if(sz(rows[i]) == 1) opers.emplace('R', i), occr[i] = 1; for(int i = 1; i <= m; i++) if(sz(cols[i]) == 1) opers.emplace('K', i), occc[i] = 1; vector<tuple<char,int,char>> printer; while(!opers.empty()) { auto [c, i] = opers.front(); opers.pop(); if(sz(c == 'R'? rows[i] : cols[i]) == 0) continue; printer.emplace_back(c, i, (c == 'R'? rows[i].a.begin() -> first : cols[i].a.begin() -> first)); //cerr << c << ' ' << i << ' ' << get<2>(printer.back()) << '\n'; if(c == 'R') { for(int j = 1; j <= m; j++) { if(mat[i][j] != 0) cols[j].erase(mat[i][j]), mat[i][j] = 0; if(occc[j] == 0 && sz(cols[j]) == 1) opers.emplace('K', j), occc[j] = 1; } } else { for(int j = 1; j <= n; j++) { if(mat[j][i] != 0) rows[j].erase(mat[j][i]), mat[j][i] = 0; if(occr[j] == 0 && sz(rows[j]) == 1) opers.emplace('R', j), occr[j] = 1; } } } cout << sz(printer) << '\n'; for(auto [a, b, c] : printer | views::reverse) cout << a << ' ' << b << ' ' << c << '\n'; } /** Anul asta nu se da centroid -- Rugaciunile mele */
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 | #include <bits/stdc++.h> #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long; using ld = long double; //#define int ll #define sz(x) ((int)(x).size()) using pii = pair<int,int>; using tii = tuple<int,int,int>; const int nmax = 2e3 + 5; char mat[nmax][nmax]; struct Map { unordered_map<char,int> a; void emplace(char x) { a[x]++; } void erase(int x) { a[x]--; if(a[x] == 0) a.erase(x); return; } int size() { return sz(a); } }; Map rows[nmax], cols[nmax]; int occr[nmax], occc[nmax]; signed main() { cin.tie(0) -> sync_with_stdio(0); int n, m; cin >> n >> m; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { cin >> mat[i][j]; rows[i].emplace(mat[i][j]); cols[j].emplace(mat[i][j]); } } queue<pair<char, int>> opers; for(int i = 1; i <= n; i++) if(sz(rows[i]) == 1) opers.emplace('R', i), occr[i] = 1; for(int i = 1; i <= m; i++) if(sz(cols[i]) == 1) opers.emplace('K', i), occc[i] = 1; vector<tuple<char,int,char>> printer; while(!opers.empty()) { auto [c, i] = opers.front(); opers.pop(); if(sz(c == 'R'? rows[i] : cols[i]) == 0) continue; printer.emplace_back(c, i, (c == 'R'? rows[i].a.begin() -> first : cols[i].a.begin() -> first)); //cerr << c << ' ' << i << ' ' << get<2>(printer.back()) << '\n'; if(c == 'R') { for(int j = 1; j <= m; j++) { if(mat[i][j] != 0) cols[j].erase(mat[i][j]), mat[i][j] = 0; if(occc[j] == 0 && sz(cols[j]) == 1) opers.emplace('K', j), occc[j] = 1; } } else { for(int j = 1; j <= n; j++) { if(mat[j][i] != 0) rows[j].erase(mat[j][i]), mat[j][i] = 0; if(occr[j] == 0 && sz(rows[j]) == 1) opers.emplace('R', j), occr[j] = 1; } } } cout << sz(printer) << '\n'; for(auto [a, b, c] : printer | views::reverse) cout << a << ' ' << b << ' ' << c << '\n'; } /** Anul asta nu se da centroid -- Rugaciunile mele */ |