#include <iostream> #include <ranges> #include <vector> #include <unordered_map> #include <tuple> #include <set> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector<string> image(n); for (int i = 0; i < n; i++) { cin >> image[i]; } vector<unordered_map<char, int>> rows(n); vector<unordered_map<char, int>> columns(m); for (int row = 0; row < n; row++) { for (int col = 0; col < m; col++) { rows[row][image[row][col]]++; columns[col][image[row][col]]++; } } set<int> columns_to_check; for (int col = 0; col < m; col++) { columns_to_check.insert(col); } set<int> rows_to_check; for (int row = 0; row < n; row++) { rows_to_check.insert(row); } vector<tuple<char, int, char>> instructions; while (!rows_to_check.empty() || !columns_to_check.empty()) { for (const auto &row: rows_to_check) { if (rows[row].empty()) { rows_to_check.erase(row); break; } if (rows[row].size() <= 1) { rows_to_check.erase(row); char k = rows[row].begin()->first; for (int col = 0; col < m; col++) { columns[col][k]--; if (columns[col][k] == 0) { columns[col].erase(k); } } instructions.emplace_back('R', row + 1, k); break; } } for (const auto &col: columns_to_check) { if (columns[col].empty()) { columns_to_check.erase(col); break; } if (columns[col].size() == 1) { columns_to_check.erase(col); char k = columns[col].begin()->first; for (int row = 0; row < n; row++) { rows[row][k]--; if (rows[row][k] == 0) { rows[row].erase(k); } } instructions.emplace_back('K', col + 1, k); break; } } } cout << instructions.size() << endl; for (auto instruction: ranges::reverse_view(instructions)) { cout << get<0>(instruction) << " " << get<1>(instruction) << " " << get<2>(instruction) << endl; } 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 | #include <iostream> #include <ranges> #include <vector> #include <unordered_map> #include <tuple> #include <set> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector<string> image(n); for (int i = 0; i < n; i++) { cin >> image[i]; } vector<unordered_map<char, int>> rows(n); vector<unordered_map<char, int>> columns(m); for (int row = 0; row < n; row++) { for (int col = 0; col < m; col++) { rows[row][image[row][col]]++; columns[col][image[row][col]]++; } } set<int> columns_to_check; for (int col = 0; col < m; col++) { columns_to_check.insert(col); } set<int> rows_to_check; for (int row = 0; row < n; row++) { rows_to_check.insert(row); } vector<tuple<char, int, char>> instructions; while (!rows_to_check.empty() || !columns_to_check.empty()) { for (const auto &row: rows_to_check) { if (rows[row].empty()) { rows_to_check.erase(row); break; } if (rows[row].size() <= 1) { rows_to_check.erase(row); char k = rows[row].begin()->first; for (int col = 0; col < m; col++) { columns[col][k]--; if (columns[col][k] == 0) { columns[col].erase(k); } } instructions.emplace_back('R', row + 1, k); break; } } for (const auto &col: columns_to_check) { if (columns[col].empty()) { columns_to_check.erase(col); break; } if (columns[col].size() == 1) { columns_to_check.erase(col); char k = columns[col].begin()->first; for (int row = 0; row < n; row++) { rows[row][k]--; if (rows[row][k] == 0) { rows[row].erase(k); } } instructions.emplace_back('K', col + 1, k); break; } } } cout << instructions.size() << endl; for (auto instruction: ranges::reverse_view(instructions)) { cout << get<0>(instruction) << " " << get<1>(instruction) << " " << get<2>(instruction) << endl; } return 0; } |