#include <bits/stdc++.h> using namespace std; #define f first #define s second constexpr int maxn = 2002; int n, m, k[maxn][30], r[maxn][30]; char tab[maxn][maxn], alf[] = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ"; queue<pair<char,int> > kan; vector<pair<pair<char,int>, char> > mv; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; ++i){ for (int j = 1; j <= m; ++j){ cin >> tab[i][j]; ++r[i][tab[i][j]-'A'+1]; ++k[j][tab[i][j]-'A'+1]; if (r[i][tab[i][j]-'A'+1] == 1) ++r[i][0]; if (k[j][tab[i][j]-'A'+1] == 1) ++k[j][0]; } } for (int i = 1; i <= n; ++i) if (r[i][0] == 1) kan.push({'R',i}); for (int i = 1; i <= m; ++i) if (k[i][0] == 1) kan.push({'K',i}); char c; while (!kan.empty()){ auto u = kan.front(); kan.pop(); if (u.f == 'R'){ if (r[u.s][0] == 0) continue; for (int i = 1; i <= 'Z'-'A'+1; ++i){ if (r[u.s][i]){ c = alf[i]; } r[u.s][i] = 0; } mv.push_back({{'R',u.s},c}); r[u.s][0] = 0; for (int i = 1; i <= m; ++i){ if (k[i][c-'A'+1]){ --k[i][c-'A'+1]; if (k[i][c-'A'+1] == 0){ --k[i][0]; if (k[i][0] == 1) kan.push({'K',i}); } } } } if (u.f == 'K'){ if (k[u.s][0] == 0) continue; for (int i = 1; i <= 'Z'-'A'+1; ++i){ if (k[u.s][i]){ c = alf[i]; } k[u.s][i] = 0; } mv.push_back({{'K',u.s},c}); k[u.s][0] = 0; for (int i = 1; i <= n; ++i){ if (r[i][c-'A'+1]){ --r[i][c-'A'+1]; if (r[i][c-'A'+1] == 0){ --r[i][0]; if (r[i][0] == 1) kan.push({'R',i}); } } } } } cout << mv.size() << '\n'; while (!mv.empty()){ cout << mv[mv.size()-1].f.f << ' ' << mv[mv.size()-1].f.s << ' ' << mv[mv.size()-1].s << '\n'; mv.pop_back(); } }
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 | #include <bits/stdc++.h> using namespace std; #define f first #define s second constexpr int maxn = 2002; int n, m, k[maxn][30], r[maxn][30]; char tab[maxn][maxn], alf[] = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ"; queue<pair<char,int> > kan; vector<pair<pair<char,int>, char> > mv; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; ++i){ for (int j = 1; j <= m; ++j){ cin >> tab[i][j]; ++r[i][tab[i][j]-'A'+1]; ++k[j][tab[i][j]-'A'+1]; if (r[i][tab[i][j]-'A'+1] == 1) ++r[i][0]; if (k[j][tab[i][j]-'A'+1] == 1) ++k[j][0]; } } for (int i = 1; i <= n; ++i) if (r[i][0] == 1) kan.push({'R',i}); for (int i = 1; i <= m; ++i) if (k[i][0] == 1) kan.push({'K',i}); char c; while (!kan.empty()){ auto u = kan.front(); kan.pop(); if (u.f == 'R'){ if (r[u.s][0] == 0) continue; for (int i = 1; i <= 'Z'-'A'+1; ++i){ if (r[u.s][i]){ c = alf[i]; } r[u.s][i] = 0; } mv.push_back({{'R',u.s},c}); r[u.s][0] = 0; for (int i = 1; i <= m; ++i){ if (k[i][c-'A'+1]){ --k[i][c-'A'+1]; if (k[i][c-'A'+1] == 0){ --k[i][0]; if (k[i][0] == 1) kan.push({'K',i}); } } } } if (u.f == 'K'){ if (k[u.s][0] == 0) continue; for (int i = 1; i <= 'Z'-'A'+1; ++i){ if (k[u.s][i]){ c = alf[i]; } k[u.s][i] = 0; } mv.push_back({{'K',u.s},c}); k[u.s][0] = 0; for (int i = 1; i <= n; ++i){ if (r[i][c-'A'+1]){ --r[i][c-'A'+1]; if (r[i][c-'A'+1] == 0){ --r[i][0]; if (r[i][0] == 1) kan.push({'R',i}); } } } } } cout << mv.size() << '\n'; while (!mv.empty()){ cout << mv[mv.size()-1].f.f << ' ' << mv[mv.size()-1].f.s << ' ' << mv[mv.size()-1].s << '\n'; mv.pop_back(); } } |