#include <iostream> #include <vector> #include <string> #include <set> #include <map> using namespace std; int main(int argc, char* argv[]) { #ifdef _DEBUG freopen("input.txt", "rt", stdin); #endif vector<string> res; cin.sync_with_stdio(false); cout.sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<string> data(n); for (int i = 0; i < n; i++) cin >> data[i]; vector < map<char, set<int>>> col2idx_hor(n), col2idx_vert(m); set<int> ready_hor, ready_vert; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { col2idx_hor[i][data[i][j]].insert(j); } if (col2idx_hor[i].size() == 1) ready_hor.insert(i); } for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { col2idx_vert[j][data[i][j]].insert(i); } if (col2idx_vert[j].size() == 1) ready_vert.insert(j); } do { for (int i : ready_hor) { if (!col2idx_hor[i].empty() && !col2idx_hor[i].begin()->second.empty()) { res.push_back("R " + to_string(i + 1) + " " + col2idx_hor[i].begin()->first); for (int j : col2idx_hor[i].begin()->second) { char color = data[i][j]; #ifdef _DEBUG if (data[i][j] != col2idx_hor[i].begin()->first) _DEBUG_ERROR("data[i][j] != col2idx_hor[i].begin()->first"); if (data[i][j] == '?') _DEBUG_ERROR("(data[i][j] == '?')"); #endif col2idx_vert[j][color].erase(i); if (col2idx_vert[j][color].empty()) { col2idx_vert[j].erase(color); if (col2idx_vert[j].size() == 1) ready_vert.insert(j); } #ifdef _DEBUG data[i][j] = '?'; #endif } } } ready_hor.clear(); for (int j : ready_vert) { if (!col2idx_vert[j].empty() && !col2idx_vert[j].begin()->second.empty()) { res.push_back("K " + to_string(j + 1) + " " + col2idx_vert[j].begin()->first); for (int i : col2idx_vert[j].begin()->second) { char color = data[i][j]; #ifdef _DEBUG if (data[i][j] != col2idx_vert[j].begin()->first) _DEBUG_ERROR("data[i][j] != col2idx_vert[j].begin()->first"); if (data[i][j] == '?') _DEBUG_ERROR("(data[i][j] == '?')"); #endif col2idx_hor[i][color].erase(j); if (col2idx_hor[i][color].empty()) { col2idx_hor[i].erase(color); if (col2idx_hor[i].size()==1) ready_hor.insert(i); } #ifdef _DEBUG data[i][j] = '?'; #endif } } } ready_vert.clear(); } while (!ready_hor.empty()); #ifdef _DEBUG for (int i = 0; i < n; i++) if (col2idx_hor[i].size() > 1) { cout << "NIEMOZLIWO!!!"; return -1; }; for (int j = 0; j < m; j++) if (col2idx_vert[j].size() > 1) { cout << "NIEMOZLIWO!!!"; return -1; }; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (data[i][j] != '?') { cout << "NIEMOZLIWO!!!"; return -1; }; #endif cout << res.size() << "\n"; for (vector<string>::reverse_iterator rit = res.rbegin(); rit != res.rend(); rit++) cout << (*rit) << "\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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | #include <iostream> #include <vector> #include <string> #include <set> #include <map> using namespace std; int main(int argc, char* argv[]) { #ifdef _DEBUG freopen("input.txt", "rt", stdin); #endif vector<string> res; cin.sync_with_stdio(false); cout.sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<string> data(n); for (int i = 0; i < n; i++) cin >> data[i]; vector < map<char, set<int>>> col2idx_hor(n), col2idx_vert(m); set<int> ready_hor, ready_vert; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { col2idx_hor[i][data[i][j]].insert(j); } if (col2idx_hor[i].size() == 1) ready_hor.insert(i); } for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { col2idx_vert[j][data[i][j]].insert(i); } if (col2idx_vert[j].size() == 1) ready_vert.insert(j); } do { for (int i : ready_hor) { if (!col2idx_hor[i].empty() && !col2idx_hor[i].begin()->second.empty()) { res.push_back("R " + to_string(i + 1) + " " + col2idx_hor[i].begin()->first); for (int j : col2idx_hor[i].begin()->second) { char color = data[i][j]; #ifdef _DEBUG if (data[i][j] != col2idx_hor[i].begin()->first) _DEBUG_ERROR("data[i][j] != col2idx_hor[i].begin()->first"); if (data[i][j] == '?') _DEBUG_ERROR("(data[i][j] == '?')"); #endif col2idx_vert[j][color].erase(i); if (col2idx_vert[j][color].empty()) { col2idx_vert[j].erase(color); if (col2idx_vert[j].size() == 1) ready_vert.insert(j); } #ifdef _DEBUG data[i][j] = '?'; #endif } } } ready_hor.clear(); for (int j : ready_vert) { if (!col2idx_vert[j].empty() && !col2idx_vert[j].begin()->second.empty()) { res.push_back("K " + to_string(j + 1) + " " + col2idx_vert[j].begin()->first); for (int i : col2idx_vert[j].begin()->second) { char color = data[i][j]; #ifdef _DEBUG if (data[i][j] != col2idx_vert[j].begin()->first) _DEBUG_ERROR("data[i][j] != col2idx_vert[j].begin()->first"); if (data[i][j] == '?') _DEBUG_ERROR("(data[i][j] == '?')"); #endif col2idx_hor[i][color].erase(j); if (col2idx_hor[i][color].empty()) { col2idx_hor[i].erase(color); if (col2idx_hor[i].size()==1) ready_hor.insert(i); } #ifdef _DEBUG data[i][j] = '?'; #endif } } } ready_vert.clear(); } while (!ready_hor.empty()); #ifdef _DEBUG for (int i = 0; i < n; i++) if (col2idx_hor[i].size() > 1) { cout << "NIEMOZLIWO!!!"; return -1; }; for (int j = 0; j < m; j++) if (col2idx_vert[j].size() > 1) { cout << "NIEMOZLIWO!!!"; return -1; }; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (data[i][j] != '?') { cout << "NIEMOZLIWO!!!"; return -1; }; #endif cout << res.size() << "\n"; for (vector<string>::reverse_iterator rit = res.rbegin(); rit != res.rend(); rit++) cout << (*rit) << "\n"; return 0; } |