#include <iostream> #include <vector> #include <queue> #include <stack> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<vector<int>> wiersze (26, vector<int> (n)); vector<vector<int>> kolumny (26, vector<int> (m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char a; cin >> a; wiersze[int(a)-65][i]++; kolumny[int(a)-65][j]++; } } vector<int> Wkolory(n); vector<int> Kkolory(m); queue<pair<bool, pair<int, int>>> usuwanie; stack<pair<bool, pair<int, int>>> output; for (int i = 0; i < n; i++) { int l = 0; int c; for (int j = 0; j < 26; j++) { if (wiersze[j][i] != 0) { l++; c = j; } } Wkolory[i] = l; // wiersz(rzad) = 0, kolumna = 1 if (l == 1) { usuwanie.push(make_pair(0, make_pair(i, c))); } } for (int i = 0; i < m; i++) { int l = 0; int c; for (int j = 0; j < 26; j++) { if (kolumny[j][i] != 0) { l++; c = j; } } Kkolory[i] = l; // wiersz(rzad) = 0, kolumna = 1 if (l == 1) { usuwanie.push(make_pair(1, make_pair(i, c))); } } while (!usuwanie.empty()) { output.push(usuwanie.front()); if (usuwanie.front().first == 0) { for (int i = 0; i < m; i++) { kolumny[usuwanie.front().second.second][i]--; if (kolumny[usuwanie.front().second.second][i] == 0) { Kkolory[i]--; if (Kkolory[i] == 1) { for (int j = 0; j < 26; j++) { if (kolumny[j][i] != 0) { usuwanie.push(make_pair(1, make_pair(i, j))); break; } } } } } } else { for (int i = 0; i < n; i++) { wiersze[usuwanie.front().second.second][i]--; if (wiersze[usuwanie.front().second.second][i] == 0) { Wkolory[i]--; if (Kkolory[i] == 1) { for (int j = 0; j < 26; j++) { if (wiersze[j][i] != 0) { usuwanie.push(make_pair(0, make_pair(i, j))); break; } } } } } } usuwanie.pop(); } cout << output.size() << endl; while (!output.empty()) { if (output.top().first == 0) { cout << "R "; } else { cout << "K "; } cout << output.top().second.first + 1 << " " << char(output.top().second.second+65) << endl; output.pop(); } 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 | #include <iostream> #include <vector> #include <queue> #include <stack> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<vector<int>> wiersze (26, vector<int> (n)); vector<vector<int>> kolumny (26, vector<int> (m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char a; cin >> a; wiersze[int(a)-65][i]++; kolumny[int(a)-65][j]++; } } vector<int> Wkolory(n); vector<int> Kkolory(m); queue<pair<bool, pair<int, int>>> usuwanie; stack<pair<bool, pair<int, int>>> output; for (int i = 0; i < n; i++) { int l = 0; int c; for (int j = 0; j < 26; j++) { if (wiersze[j][i] != 0) { l++; c = j; } } Wkolory[i] = l; // wiersz(rzad) = 0, kolumna = 1 if (l == 1) { usuwanie.push(make_pair(0, make_pair(i, c))); } } for (int i = 0; i < m; i++) { int l = 0; int c; for (int j = 0; j < 26; j++) { if (kolumny[j][i] != 0) { l++; c = j; } } Kkolory[i] = l; // wiersz(rzad) = 0, kolumna = 1 if (l == 1) { usuwanie.push(make_pair(1, make_pair(i, c))); } } while (!usuwanie.empty()) { output.push(usuwanie.front()); if (usuwanie.front().first == 0) { for (int i = 0; i < m; i++) { kolumny[usuwanie.front().second.second][i]--; if (kolumny[usuwanie.front().second.second][i] == 0) { Kkolory[i]--; if (Kkolory[i] == 1) { for (int j = 0; j < 26; j++) { if (kolumny[j][i] != 0) { usuwanie.push(make_pair(1, make_pair(i, j))); break; } } } } } } else { for (int i = 0; i < n; i++) { wiersze[usuwanie.front().second.second][i]--; if (wiersze[usuwanie.front().second.second][i] == 0) { Wkolory[i]--; if (Kkolory[i] == 1) { for (int j = 0; j < 26; j++) { if (wiersze[j][i] != 0) { usuwanie.push(make_pair(0, make_pair(i, j))); break; } } } } } } usuwanie.pop(); } cout << output.size() << endl; while (!output.empty()) { if (output.top().first == 0) { cout << "R "; } else { cout << "K "; } cout << output.top().second.first + 1 << " " << char(output.top().second.second+65) << endl; output.pop(); } return 0; } |