#include <bits/stdc++.h> using namespace std; #define endl '\n' #define L long long #define MP make_pair #define REP(i, n) for(int i = 0; i < n; ++i) #define REPR(i, n) for(int i = n - 1; i >= 0; --i) #define FOR(i, a, b) for(int i = a; i < b; ++i) #define FORR(i, a, b) for(int i = b - 1; i >= a; --i) #define EB emplace_back #define ST first #define ND second #define S size #define RS resize template<class T> using P = pair<T, T>; template<class T> using V = vector<T>; struct Line { bool colored = false; unordered_map<char, int> col_occ; }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; V<string> p(n); REP(i, n) { cin >> p[i]; } V<Line> rows(n); REP(i, n) { REP(j, m) { char c = p[i][j]; rows[i].col_occ[c]++; } } V<Line> columns(m); REP(i, n) { REP(j, m) { char c = p[i][j]; columns[j].col_occ[c]++; } } queue<pair<bool, int>> q; REP(i, n) { if (rows[i].col_occ.size() == 1) { q.emplace(true, i); } } REP(j, m) { if (columns[j].col_occ.size() == 1) { q.emplace(false, j); } } V<tuple<char, int, char>> res; while (!q.empty()) { auto [is_row, idx] = q.front(); q.pop(); if (is_row) { rows[idx].colored = true; if (rows[idx].col_occ.size() == 0) { continue; } res.EB('R', idx + 1, rows[idx].col_occ.begin()->first); REP(j, m) { if (p[idx][j] != '-') { char c = p[idx][j]; columns[j].col_occ[c]--; if (columns[j].col_occ[c] == 0) { columns[j].col_occ.erase(c); if (columns[j].col_occ.size() == 1) { q.emplace(false, j); } } } p[idx][j] = '-'; } } else { columns[idx].colored = true; if (columns[idx].col_occ.size() == 0) { continue; } res.EB('K', idx + 1, columns[idx].col_occ.begin()->first); REP(i, n) { if (p[i][idx] != '-') { char c = p[i][idx]; rows[i].col_occ[c]--; if (rows[i].col_occ[c] == 0) { rows[i].col_occ.erase(c); if (rows[i].col_occ.size() == 1) { q.emplace(true, i); } } } p[i][idx] = '-'; } } } cout << res.size() << '\n'; reverse(res.begin(), res.end()); for (auto& [t, idx, c] : res) { cout << t << ' ' << idx << ' ' << c << '\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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #include <bits/stdc++.h> using namespace std; #define endl '\n' #define L long long #define MP make_pair #define REP(i, n) for(int i = 0; i < n; ++i) #define REPR(i, n) for(int i = n - 1; i >= 0; --i) #define FOR(i, a, b) for(int i = a; i < b; ++i) #define FORR(i, a, b) for(int i = b - 1; i >= a; --i) #define EB emplace_back #define ST first #define ND second #define S size #define RS resize template<class T> using P = pair<T, T>; template<class T> using V = vector<T>; struct Line { bool colored = false; unordered_map<char, int> col_occ; }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; V<string> p(n); REP(i, n) { cin >> p[i]; } V<Line> rows(n); REP(i, n) { REP(j, m) { char c = p[i][j]; rows[i].col_occ[c]++; } } V<Line> columns(m); REP(i, n) { REP(j, m) { char c = p[i][j]; columns[j].col_occ[c]++; } } queue<pair<bool, int>> q; REP(i, n) { if (rows[i].col_occ.size() == 1) { q.emplace(true, i); } } REP(j, m) { if (columns[j].col_occ.size() == 1) { q.emplace(false, j); } } V<tuple<char, int, char>> res; while (!q.empty()) { auto [is_row, idx] = q.front(); q.pop(); if (is_row) { rows[idx].colored = true; if (rows[idx].col_occ.size() == 0) { continue; } res.EB('R', idx + 1, rows[idx].col_occ.begin()->first); REP(j, m) { if (p[idx][j] != '-') { char c = p[idx][j]; columns[j].col_occ[c]--; if (columns[j].col_occ[c] == 0) { columns[j].col_occ.erase(c); if (columns[j].col_occ.size() == 1) { q.emplace(false, j); } } } p[idx][j] = '-'; } } else { columns[idx].colored = true; if (columns[idx].col_occ.size() == 0) { continue; } res.EB('K', idx + 1, columns[idx].col_occ.begin()->first); REP(i, n) { if (p[i][idx] != '-') { char c = p[i][idx]; rows[i].col_occ[c]--; if (rows[i].col_occ[c] == 0) { rows[i].col_occ.erase(c); if (rows[i].col_occ.size() == 1) { q.emplace(true, i); } } } p[i][idx] = '-'; } } } cout << res.size() << '\n'; reverse(res.begin(), res.end()); for (auto& [t, idx, c] : res) { cout << t << ' ' << idx << ' ' << c << '\n'; } } |