#include <iostream>
#include <unordered_map>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// Definicja funkcji find_single_color
tuple<char, int, char> find_single_color(unordered_map<int, map<char, int>>& rows, unordered_map<int, map<char, int>>& cols) {
for (auto& row : rows) {
if (row.second.size() == 1) {
return make_tuple('R', row.first, row.second.begin()->first);
}
}
for (auto& col : cols) {
if (col.second.size() == 1) {
return make_tuple('K', col.first, col.second.begin()->first);
}
}
return make_tuple('\0', -1, '\0');
}
int main() {
int n, m;
cin >> n >> m;
unordered_map<int, map<char, int>> rows;
unordered_map<int, map<char, int>> cols;
for (int i = 0; i < n; ++i) {
string row;
cin >> row;
for (int j = 0; j < m; ++j) {
rows[i][row[j]]++;
cols[j][row[j]]++;
}
}
vector<tuple<char, int, char>> operations;
while (!cols.empty() || !rows.empty()) {
char row_or_col;
int no;
char color;
tie(row_or_col, no, color) = find_single_color(rows, cols);
if (row_or_col == 'K') {
cols.erase(no);
for (auto it = rows.begin(); it != rows.end();) {
auto& counter = it->second;
counter[color]--;
if (counter[color] == 0) {
counter.erase(color);
}
if (counter.empty()) {
it = rows.erase(it);
} else {
++it;
}
}
} else {
rows.erase(no);
for (auto it = cols.begin(); it != cols.end();) {
auto& counter = it->second;
counter[color]--;
if (counter[color] == 0) {
counter.erase(color);
}
if (counter.empty()) {
it = cols.erase(it);
} else {
++it;
}
}
}
operations.push_back(make_tuple(row_or_col, no + 1, color));
}
reverse(operations.begin(), operations.end());
cout << operations.size() << endl;
for (const auto& op : operations) {
cout << get<0>(op) << " " << get<1>(op) << " " << get<2>(op) << 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 | #include <iostream> #include <unordered_map> #include <map> #include <vector> #include <string> #include <algorithm> using namespace std; // Definicja funkcji find_single_color tuple<char, int, char> find_single_color(unordered_map<int, map<char, int>>& rows, unordered_map<int, map<char, int>>& cols) { for (auto& row : rows) { if (row.second.size() == 1) { return make_tuple('R', row.first, row.second.begin()->first); } } for (auto& col : cols) { if (col.second.size() == 1) { return make_tuple('K', col.first, col.second.begin()->first); } } return make_tuple('\0', -1, '\0'); } int main() { int n, m; cin >> n >> m; unordered_map<int, map<char, int>> rows; unordered_map<int, map<char, int>> cols; for (int i = 0; i < n; ++i) { string row; cin >> row; for (int j = 0; j < m; ++j) { rows[i][row[j]]++; cols[j][row[j]]++; } } vector<tuple<char, int, char>> operations; while (!cols.empty() || !rows.empty()) { char row_or_col; int no; char color; tie(row_or_col, no, color) = find_single_color(rows, cols); if (row_or_col == 'K') { cols.erase(no); for (auto it = rows.begin(); it != rows.end();) { auto& counter = it->second; counter[color]--; if (counter[color] == 0) { counter.erase(color); } if (counter.empty()) { it = rows.erase(it); } else { ++it; } } } else { rows.erase(no); for (auto it = cols.begin(); it != cols.end();) { auto& counter = it->second; counter[color]--; if (counter[color] == 0) { counter.erase(color); } if (counter.empty()) { it = cols.erase(it); } else { ++it; } } } operations.push_back(make_tuple(row_or_col, no + 1, color)); } reverse(operations.begin(), operations.end()); cout << operations.size() << endl; for (const auto& op : operations) { cout << get<0>(op) << " " << get<1>(op) << " " << get<2>(op) << endl; } return 0; } |
English