#include <iostream> #include <vector> #include <algorithm> #include <cmath> #define st first #define nd second typedef long long ll; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<vector<char>> c(n, vector<char>(m)); vector<vector<int>> cnt_cols(m, vector<int>(27)); vector<vector<int>> cnt_rows(n, vector<int>(27)); vector<int> k_cols(m), k_rows(n); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> c[i][j]; cnt_rows[i][c[i][j]-'A']++; if (cnt_rows[i][c[i][j]-'A'] == 1) k_rows[i]++; cnt_cols[j][c[i][j]-'A']++; if (cnt_cols[j][c[i][j]-'A'] == 1) k_cols[j]++; } } vector<int> single_cols, single_rows; for (int i = 0; i < n; i++) { if (k_rows[i] == 1) single_rows.push_back(i); } for (int i = 0; i < m; i++) { if (k_cols[i] == 1) single_cols.push_back(i); } // for (int x: single_cols) cout << x << ' '; // cout << '\n'; // for (int x: single_rows) cout << x << ' '; // cout << '\n'; vector<pair<char, pair<int, char>>> ans; while (!single_cols.empty() || !single_rows.empty()) { for (auto col: single_cols) { for (int i = 0; i < 27; i++) { if (cnt_cols[col][i] > 0) { ans.push_back({'K', {col+1, (char)i+'A'}}); break; } } for (int i = 0; i < n; i++) { char cur = c[i][col]; if (cur == '#') continue; cur -= 'A'; cnt_rows[i][cur]--; if (cnt_rows[i][cur] == 0) { k_rows[i]--; if (k_rows[i] == 1) { single_rows.push_back(i); } } } } single_cols.clear(); for (auto row: single_rows) { for (int i = 0; i < 27; i++) { if (cnt_rows[row][i] > 0) { ans.push_back({'R', {row+1, (char)i+'A'}}); break; } } for (int i = 0; i < m; i++) { char cur = c[row][i]; if (cur == '#') continue; cur -= 'A'; cnt_cols[i][cur]--; if (cnt_cols[i][cur] == 0) { k_cols[i]--; if (k_cols[i] == 1) { single_cols.push_back(i); } } } } single_rows.clear(); } // for (int x: single_rows) cout << x << ' '; cout << ans.size() << '\n'; for (int i = ans.size()-1; i >= 0; i--) { cout << ans[i].st << ' ' << ans[i].nd.st << ' ' << ans[i].nd.nd << '\n'; } 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 | #include <iostream> #include <vector> #include <algorithm> #include <cmath> #define st first #define nd second typedef long long ll; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<vector<char>> c(n, vector<char>(m)); vector<vector<int>> cnt_cols(m, vector<int>(27)); vector<vector<int>> cnt_rows(n, vector<int>(27)); vector<int> k_cols(m), k_rows(n); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> c[i][j]; cnt_rows[i][c[i][j]-'A']++; if (cnt_rows[i][c[i][j]-'A'] == 1) k_rows[i]++; cnt_cols[j][c[i][j]-'A']++; if (cnt_cols[j][c[i][j]-'A'] == 1) k_cols[j]++; } } vector<int> single_cols, single_rows; for (int i = 0; i < n; i++) { if (k_rows[i] == 1) single_rows.push_back(i); } for (int i = 0; i < m; i++) { if (k_cols[i] == 1) single_cols.push_back(i); } // for (int x: single_cols) cout << x << ' '; // cout << '\n'; // for (int x: single_rows) cout << x << ' '; // cout << '\n'; vector<pair<char, pair<int, char>>> ans; while (!single_cols.empty() || !single_rows.empty()) { for (auto col: single_cols) { for (int i = 0; i < 27; i++) { if (cnt_cols[col][i] > 0) { ans.push_back({'K', {col+1, (char)i+'A'}}); break; } } for (int i = 0; i < n; i++) { char cur = c[i][col]; if (cur == '#') continue; cur -= 'A'; cnt_rows[i][cur]--; if (cnt_rows[i][cur] == 0) { k_rows[i]--; if (k_rows[i] == 1) { single_rows.push_back(i); } } } } single_cols.clear(); for (auto row: single_rows) { for (int i = 0; i < 27; i++) { if (cnt_rows[row][i] > 0) { ans.push_back({'R', {row+1, (char)i+'A'}}); break; } } for (int i = 0; i < m; i++) { char cur = c[row][i]; if (cur == '#') continue; cur -= 'A'; cnt_cols[i][cur]--; if (cnt_cols[i][cur] == 0) { k_cols[i]--; if (k_cols[i] == 1) { single_cols.push_back(i); } } } } single_rows.clear(); } // for (int x: single_rows) cout << x << ' '; cout << ans.size() << '\n'; for (int i = ans.size()-1; i >= 0; i--) { cout << ans[i].st << ' ' << ans[i].nd.st << ' ' << ans[i].nd.nd << '\n'; } return 0; } |