#include <algorithm> #include <iostream> #include <queue> #include <vector> #include <array> using namespace std; struct to_paint { char direction; int idx; int color; }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<vector<int>> board(n, vector<int>(m)); vector<array<int, 26>> colors_in_rows(n); vector<array<int, 26>> colors_in_columns(m); char color; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> color; board[i][j] = color - 'A'; colors_in_rows[i][color - 'A']++; colors_in_columns[j][color - 'A']++; } } vector<int> zeros_in_rows(n); vector<int> zeros_in_columns(m); queue<to_paint> to_process; for (int i = 0; i < n; i++) { for (int j = 0; j < 26; j++) { zeros_in_rows[i] += (colors_in_rows[i][j] == 0); } if (zeros_in_rows[i] == 25) { for (int j = 0; j < 26; j++) { if (colors_in_rows[i][j] != 0) to_process.push({'R', i, j}); } } } for (int i = 0; i < m; i++) { for (int j = 0; j < 26; j++) { zeros_in_columns[i] += (colors_in_columns[i][j] == 0); } if (zeros_in_columns[i] == 25) { for (int j = 0; j < 26; j++) { if (colors_in_columns[i][j] != 0) to_process.push({'K', i, j}); } } } vector<to_paint> to_print; while (!to_process.empty()) { to_paint one_to_paint = to_process.front(); to_print.push_back(to_process.front()); to_process.pop(); if (one_to_paint.direction == 'R') { for (int i = 0; i < m; i++) { if(--colors_in_columns[i][one_to_paint.color] == 0) { if (++zeros_in_columns[i] == 25) { for (int j = 0; j < 26; j++) { if (colors_in_columns[i][j] > 0) to_process.push({'K', i, j}); } } } } } else { for (int i = 0; i < n; i++) { if(--colors_in_rows[i][one_to_paint.color] == 0) { if (++zeros_in_rows[i] == 25) { for (int j = 0; j < 26; j++) { if (colors_in_rows[i][j] > 0) to_process.push({'R', i, j}); } } } } } } reverse(to_print.begin(), to_print.end()); cout << to_print.size() << '\n'; for (auto &one_to_print : to_print) { cout << one_to_print.direction << ' ' << one_to_print.idx + 1 << ' ' << char(one_to_print.color + 'A') << '\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 | #include <algorithm> #include <iostream> #include <queue> #include <vector> #include <array> using namespace std; struct to_paint { char direction; int idx; int color; }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<vector<int>> board(n, vector<int>(m)); vector<array<int, 26>> colors_in_rows(n); vector<array<int, 26>> colors_in_columns(m); char color; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> color; board[i][j] = color - 'A'; colors_in_rows[i][color - 'A']++; colors_in_columns[j][color - 'A']++; } } vector<int> zeros_in_rows(n); vector<int> zeros_in_columns(m); queue<to_paint> to_process; for (int i = 0; i < n; i++) { for (int j = 0; j < 26; j++) { zeros_in_rows[i] += (colors_in_rows[i][j] == 0); } if (zeros_in_rows[i] == 25) { for (int j = 0; j < 26; j++) { if (colors_in_rows[i][j] != 0) to_process.push({'R', i, j}); } } } for (int i = 0; i < m; i++) { for (int j = 0; j < 26; j++) { zeros_in_columns[i] += (colors_in_columns[i][j] == 0); } if (zeros_in_columns[i] == 25) { for (int j = 0; j < 26; j++) { if (colors_in_columns[i][j] != 0) to_process.push({'K', i, j}); } } } vector<to_paint> to_print; while (!to_process.empty()) { to_paint one_to_paint = to_process.front(); to_print.push_back(to_process.front()); to_process.pop(); if (one_to_paint.direction == 'R') { for (int i = 0; i < m; i++) { if(--colors_in_columns[i][one_to_paint.color] == 0) { if (++zeros_in_columns[i] == 25) { for (int j = 0; j < 26; j++) { if (colors_in_columns[i][j] > 0) to_process.push({'K', i, j}); } } } } } else { for (int i = 0; i < n; i++) { if(--colors_in_rows[i][one_to_paint.color] == 0) { if (++zeros_in_rows[i] == 25) { for (int j = 0; j < 26; j++) { if (colors_in_rows[i][j] > 0) to_process.push({'R', i, j}); } } } } } } reverse(to_print.begin(), to_print.end()); cout << to_print.size() << '\n'; for (auto &one_to_print : to_print) { cout << one_to_print.direction << ' ' << one_to_print.idx + 1 << ' ' << char(one_to_print.color + 'A') << '\n'; } return 0; } |