#include <iostream> #include <vector> #include <algorithm> #include <unordered_map> int n, m; char ** t; void input() { std::cin >> n >> m; t = new char*[n]; for (int i = 0; i < n; i++) { t[i] = new char[m]; } std::string s; for (int i = 0; i < n; i++) { std::cin >> s; for (int j = 0; j < m; j++) { t[i][j] = s[j]; } } } struct Operation { char direction; int number; char color; }; void solve() { std::vector<Operation> ops; std::vector< std::unordered_map<char, int> > rows(n); std::vector< std::unordered_map<char, int> > columns(m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { rows[i][t[i][j]]++; columns[j][t[i][j]]++; } } int rows_done = 0, columns_done = 0; while (rows_done != n || columns_done != m) { for (int i = 0; i < n; i++) { if (rows[i].size() == 0) { continue; } if (rows[i].size() == 1) { char c; for (auto e : rows[i]) { c = e.first; } for (int j = 0; j < m; j++) { char prev = t[i][j]; if (prev != '*') { t[i][j] = '*'; columns[j][prev]--; if (columns[j][prev] == 0) { columns[j].erase(prev); if (columns[j].size() == 0) { columns_done++; } } } } rows[i].clear(); ops.push_back({'R', i + 1, c}); rows_done++; } } for (int j = 0; j < m; j++) { if (columns[j].size() == 0) { continue; } if (columns[j].size() == 1) { char c; for (auto e : columns[j]) { c = e.first; } for (int i = 0; i < n; i++) { char prev = t[i][j]; if (prev != '*') { t[i][j] = '*'; rows[i][prev]--; if (rows[i][prev] == 0) { rows[i].erase(prev); if (rows[i].size() == 0) { rows_done++; } } } } columns[j].clear(); ops.push_back({'K', j + 1, c}); columns_done++; } } } std::reverse(ops.begin(), ops.end()); std::cout << ops.size() << "\n"; for (auto op : ops) { std::cout << op.direction << " " << op.number << " " << op.color << "\n"; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); input(); solve(); 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 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 118 | #include <iostream> #include <vector> #include <algorithm> #include <unordered_map> int n, m; char ** t; void input() { std::cin >> n >> m; t = new char*[n]; for (int i = 0; i < n; i++) { t[i] = new char[m]; } std::string s; for (int i = 0; i < n; i++) { std::cin >> s; for (int j = 0; j < m; j++) { t[i][j] = s[j]; } } } struct Operation { char direction; int number; char color; }; void solve() { std::vector<Operation> ops; std::vector< std::unordered_map<char, int> > rows(n); std::vector< std::unordered_map<char, int> > columns(m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { rows[i][t[i][j]]++; columns[j][t[i][j]]++; } } int rows_done = 0, columns_done = 0; while (rows_done != n || columns_done != m) { for (int i = 0; i < n; i++) { if (rows[i].size() == 0) { continue; } if (rows[i].size() == 1) { char c; for (auto e : rows[i]) { c = e.first; } for (int j = 0; j < m; j++) { char prev = t[i][j]; if (prev != '*') { t[i][j] = '*'; columns[j][prev]--; if (columns[j][prev] == 0) { columns[j].erase(prev); if (columns[j].size() == 0) { columns_done++; } } } } rows[i].clear(); ops.push_back({'R', i + 1, c}); rows_done++; } } for (int j = 0; j < m; j++) { if (columns[j].size() == 0) { continue; } if (columns[j].size() == 1) { char c; for (auto e : columns[j]) { c = e.first; } for (int i = 0; i < n; i++) { char prev = t[i][j]; if (prev != '*') { t[i][j] = '*'; rows[i][prev]--; if (rows[i][prev] == 0) { rows[i].erase(prev); if (rows[i].size() == 0) { rows_done++; } } } } columns[j].clear(); ops.push_back({'K', j + 1, c}); columns_done++; } } } std::reverse(ops.begin(), ops.end()); std::cout << ops.size() << "\n"; for (auto op : ops) { std::cout << op.direction << " " << op.number << " " << op.color << "\n"; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); input(); solve(); return 0; } |