#include <iostream> #include <vector> #include <unordered_map> using namespace std; int n, m; vector<vector<char>> picture; vector<unordered_map<char, int>> rowLetters; vector<unordered_map<char, int>> columnLetters; vector<string> moves; int nextMove() { for (int i = 0; i < n; i++) { if (rowLetters[i].size() == 1) { moves.push_back("R " + to_string(i + 1) + " " + rowLetters[i].begin()->first); for (int j = 0; j < m; j++) { columnLetters[j][picture[i][j]]--; if (columnLetters[j][picture[i][j]] < 1) { columnLetters[j].erase(picture[i][j]); } } rowLetters[i].clear(); return true; } } for (int j = 0; j < m; j++) { if (columnLetters[j].size() == 1) { moves.push_back("K " + to_string(j + 1) + " " + columnLetters[j].begin()->first); for (int i = 0; i < n; i++) { rowLetters[i][picture[i][j]]--; if (rowLetters[i][picture[i][j]] < 1) { rowLetters[i].erase(picture[i][j]); } } columnLetters[j].clear(); return true; } } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; picture.resize(n); rowLetters.resize(n); columnLetters.resize(m); string row; for (int i = 0; i < n; i++) { cin >> row; picture[i].resize(m); for (int j = 0; j < m; j++) { picture[i][j] = row[j]; rowLetters[i][row[j]]++; columnLetters[j][row[j]]++; } } while (nextMove()) { } cout << moves.size() << endl; for (int i = moves.size() - 1; i >= 0; i--) { cout << moves[i] << endl; } }
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 | #include <iostream> #include <vector> #include <unordered_map> using namespace std; int n, m; vector<vector<char>> picture; vector<unordered_map<char, int>> rowLetters; vector<unordered_map<char, int>> columnLetters; vector<string> moves; int nextMove() { for (int i = 0; i < n; i++) { if (rowLetters[i].size() == 1) { moves.push_back("R " + to_string(i + 1) + " " + rowLetters[i].begin()->first); for (int j = 0; j < m; j++) { columnLetters[j][picture[i][j]]--; if (columnLetters[j][picture[i][j]] < 1) { columnLetters[j].erase(picture[i][j]); } } rowLetters[i].clear(); return true; } } for (int j = 0; j < m; j++) { if (columnLetters[j].size() == 1) { moves.push_back("K " + to_string(j + 1) + " " + columnLetters[j].begin()->first); for (int i = 0; i < n; i++) { rowLetters[i][picture[i][j]]--; if (rowLetters[i][picture[i][j]] < 1) { rowLetters[i].erase(picture[i][j]); } } columnLetters[j].clear(); return true; } } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; picture.resize(n); rowLetters.resize(n); columnLetters.resize(m); string row; for (int i = 0; i < n; i++) { cin >> row; picture[i].resize(m); for (int j = 0; j < m; j++) { picture[i][j] = row[j]; rowLetters[i][row[j]]++; columnLetters[j][row[j]]++; } } while (nextMove()) { } cout << moves.size() << endl; for (int i = moves.size() - 1; i >= 0; i--) { cout << moves[i] << endl; } } |