#include <bits/stdc++.h> using namespace std; typedef long long LL; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<string> v(n); for (auto &e : v) cin >> e; vector<map<char, int>> cnt_rows(n), cnt_cols(m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cnt_rows[i][v[i][j]]++; } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) cnt_cols[i][v[j][i]]++; } vector<string> ans; int all = n*m; while (all > 0) { int empty_row = -1; for (int i = 0; i < n; i++) { if (cnt_rows[i].size() == 1) { empty_row = i; break; } } if (empty_row != -1) { char color = cnt_rows[empty_row].begin()->first; ans.push_back("R " + to_string(empty_row+1) + " " + color); all -= cnt_rows[empty_row].begin()->second; cnt_rows[empty_row] = map<char, int>(); for (int i = 0; i < m; i++) { cnt_cols[i][color]--; if (cnt_cols[i][color] <= 0) cnt_cols[i].erase(color); } continue; } int empty_col = -1; for (int i = 0; i < m; i++) { if (cnt_cols[i].size() == 1) { empty_col = i; break; } } if (empty_col != -1) { char color = cnt_cols[empty_col].begin()->first; ans.push_back("K " + to_string(empty_col+1) + " " + color); all -= cnt_cols[empty_col].begin()->second; cnt_cols[empty_col] = map<char, int>(); for (int i = 0; i < n; i++) { cnt_rows[i][color]--; if (cnt_rows[i][color] <= 0) cnt_rows[i].erase(color); } continue; } } reverse(ans.begin(), ans.end()); cout << ans.size() << "\n"; for (auto &e : ans) cout << e << "\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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<string> v(n); for (auto &e : v) cin >> e; vector<map<char, int>> cnt_rows(n), cnt_cols(m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cnt_rows[i][v[i][j]]++; } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) cnt_cols[i][v[j][i]]++; } vector<string> ans; int all = n*m; while (all > 0) { int empty_row = -1; for (int i = 0; i < n; i++) { if (cnt_rows[i].size() == 1) { empty_row = i; break; } } if (empty_row != -1) { char color = cnt_rows[empty_row].begin()->first; ans.push_back("R " + to_string(empty_row+1) + " " + color); all -= cnt_rows[empty_row].begin()->second; cnt_rows[empty_row] = map<char, int>(); for (int i = 0; i < m; i++) { cnt_cols[i][color]--; if (cnt_cols[i][color] <= 0) cnt_cols[i].erase(color); } continue; } int empty_col = -1; for (int i = 0; i < m; i++) { if (cnt_cols[i].size() == 1) { empty_col = i; break; } } if (empty_col != -1) { char color = cnt_cols[empty_col].begin()->first; ans.push_back("K " + to_string(empty_col+1) + " " + color); all -= cnt_cols[empty_col].begin()->second; cnt_cols[empty_col] = map<char, int>(); for (int i = 0; i < n; i++) { cnt_rows[i][color]--; if (cnt_rows[i][color] <= 0) cnt_rows[i].erase(color); } continue; } } reverse(ans.begin(), ans.end()); cout << ans.size() << "\n"; for (auto &e : ans) cout << e << "\n"; } |