#include <algorithm> #include <iostream> #include <unordered_map> #include <vector> struct xd { xd(int n, int m): grid(n, std::vector<int>(m, -1)), per_color_in_x(n), per_color_in_y(m) {} void set(int x, int y, int col) { if (grid[x][y] != -1) { unset(x, y); } grid[x][y] = col; per_color_in_x[x][col] += 1; if (per_color_in_x[x][col] == 1) { if (per_color_in_x[x].size() == 1) { one_x_to_color.emplace(x, col); } else if (per_color_in_x[x].size() == 2) { one_x_to_color.erase(x); } } per_color_in_y[y][col] += 1; if (per_color_in_y[y][col] == 1) { if (per_color_in_y[y].size() == 1) { one_y_to_color.emplace(y, col); } else if (per_color_in_y[y].size() == 2) { one_y_to_color.erase(y); } } } void unset(int x, int y) { if (grid[x][y] == -1) { return; } int col = grid[x][y]; grid[x][y] = -1; per_color_in_x[x][col] -= 1; if (per_color_in_x[x][col] == 0) { per_color_in_x[x].erase(col); if (per_color_in_x[x].size() == 1) { int remain = per_color_in_x[x].begin()->first; one_x_to_color.emplace(x, remain); } else if (per_color_in_x[x].size() == 0) { one_x_to_color.erase(x); } } per_color_in_y[y][col] -= 1; if (per_color_in_y[y][col] == 0) { per_color_in_y[y].erase(col); if (per_color_in_y[y].size() == 1) { int remain = per_color_in_y[y].begin()->first; one_y_to_color.emplace(y, remain); } else if (per_color_in_y[y].size() == 0) { one_y_to_color.erase(y); } } } std::vector<std::vector<int>> grid; std::vector<std::unordered_map<int, int>> per_color_in_x; std::vector<std::unordered_map<int, int>> per_color_in_y; std::unordered_map<int, int> one_x_to_color; std::unordered_map<int, int> one_y_to_color; }; int main() { int n, m; std::cin >> n >> m; xd xd(n, m); for (int x = 0; x < n; ++x) { std::string in; std::cin >> in; for (int y = 0; y < m; ++y) { xd.set(x, y, in[y] - 'A'); } } // std::cerr << "\n"; std::vector<std::tuple<char, int, int>> moves; for (;;) { // for (int x = 0; x < n; ++x) { // for (int y = 0; y < m; ++y) { // std::cerr << (char)('A' + xd.grid[x][y]); // } // std::cerr << "\n"; // } // std::cerr << "R: "; // for (int x = 0; x < n; ++x) { // std::cerr << x + 1 << "->" << xd.per_color_in_x[x].size() << "( "; // for (auto [col, n]: xd.per_color_in_x[x]) { // std::cerr << (char)('A' + col) << n << " "; // } // std::cerr << ") "; // } // std::cerr << "\n"; // std::cerr << "K: "; // for (int y = 0; y < m; ++y) { // std::cerr << y + 1 << "->" << xd.per_color_in_y[y].size() << "( "; // for (auto [col, n]: xd.per_color_in_y[y]) { // std::cerr << (char)('A' + col) << n << " "; // } // std::cerr << ") "; // } // std::cerr << "\n"; // std::cerr << "TODO: "; // for (auto [x, col]: xd.one_x_to_color) { // std::cerr << "(R " << x + 1 << " " << (char)('A' + col) << ") "; // } // for (auto [y, col]: xd.one_y_to_color) { // std::cerr << "(K " << y + 1 << " " << (char)('A' + col) << ") "; // } // std::cerr << "\n"; if (!xd.one_x_to_color.empty()) { auto [x, col] = *xd.one_x_to_color.begin(); moves.emplace_back('R', x, col); for (int y = 0; y < m; ++y) { xd.unset(x, y); } } else if (!xd.one_y_to_color.empty()) { auto [y, col] = *xd.one_y_to_color.begin(); moves.emplace_back('K', y, col); for (int x = 0; x < n; ++x) { xd.unset(x, y); } } else { break; } // auto [type, i, col] = moves.back(); // std::cerr << type << " " << i + 1 << " " << (char)('A' + col) << "\n"; } std::reverse(moves.begin(), moves.end()); std::cout << moves.size() << "\n"; for (auto [type, i, col]: moves) { std::cout << type << " " << i + 1 << " " << (char)('A' + col) << "\n"; } }
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #include <algorithm> #include <iostream> #include <unordered_map> #include <vector> struct xd { xd(int n, int m): grid(n, std::vector<int>(m, -1)), per_color_in_x(n), per_color_in_y(m) {} void set(int x, int y, int col) { if (grid[x][y] != -1) { unset(x, y); } grid[x][y] = col; per_color_in_x[x][col] += 1; if (per_color_in_x[x][col] == 1) { if (per_color_in_x[x].size() == 1) { one_x_to_color.emplace(x, col); } else if (per_color_in_x[x].size() == 2) { one_x_to_color.erase(x); } } per_color_in_y[y][col] += 1; if (per_color_in_y[y][col] == 1) { if (per_color_in_y[y].size() == 1) { one_y_to_color.emplace(y, col); } else if (per_color_in_y[y].size() == 2) { one_y_to_color.erase(y); } } } void unset(int x, int y) { if (grid[x][y] == -1) { return; } int col = grid[x][y]; grid[x][y] = -1; per_color_in_x[x][col] -= 1; if (per_color_in_x[x][col] == 0) { per_color_in_x[x].erase(col); if (per_color_in_x[x].size() == 1) { int remain = per_color_in_x[x].begin()->first; one_x_to_color.emplace(x, remain); } else if (per_color_in_x[x].size() == 0) { one_x_to_color.erase(x); } } per_color_in_y[y][col] -= 1; if (per_color_in_y[y][col] == 0) { per_color_in_y[y].erase(col); if (per_color_in_y[y].size() == 1) { int remain = per_color_in_y[y].begin()->first; one_y_to_color.emplace(y, remain); } else if (per_color_in_y[y].size() == 0) { one_y_to_color.erase(y); } } } std::vector<std::vector<int>> grid; std::vector<std::unordered_map<int, int>> per_color_in_x; std::vector<std::unordered_map<int, int>> per_color_in_y; std::unordered_map<int, int> one_x_to_color; std::unordered_map<int, int> one_y_to_color; }; int main() { int n, m; std::cin >> n >> m; xd xd(n, m); for (int x = 0; x < n; ++x) { std::string in; std::cin >> in; for (int y = 0; y < m; ++y) { xd.set(x, y, in[y] - 'A'); } } // std::cerr << "\n"; std::vector<std::tuple<char, int, int>> moves; for (;;) { // for (int x = 0; x < n; ++x) { // for (int y = 0; y < m; ++y) { // std::cerr << (char)('A' + xd.grid[x][y]); // } // std::cerr << "\n"; // } // std::cerr << "R: "; // for (int x = 0; x < n; ++x) { // std::cerr << x + 1 << "->" << xd.per_color_in_x[x].size() << "( "; // for (auto [col, n]: xd.per_color_in_x[x]) { // std::cerr << (char)('A' + col) << n << " "; // } // std::cerr << ") "; // } // std::cerr << "\n"; // std::cerr << "K: "; // for (int y = 0; y < m; ++y) { // std::cerr << y + 1 << "->" << xd.per_color_in_y[y].size() << "( "; // for (auto [col, n]: xd.per_color_in_y[y]) { // std::cerr << (char)('A' + col) << n << " "; // } // std::cerr << ") "; // } // std::cerr << "\n"; // std::cerr << "TODO: "; // for (auto [x, col]: xd.one_x_to_color) { // std::cerr << "(R " << x + 1 << " " << (char)('A' + col) << ") "; // } // for (auto [y, col]: xd.one_y_to_color) { // std::cerr << "(K " << y + 1 << " " << (char)('A' + col) << ") "; // } // std::cerr << "\n"; if (!xd.one_x_to_color.empty()) { auto [x, col] = *xd.one_x_to_color.begin(); moves.emplace_back('R', x, col); for (int y = 0; y < m; ++y) { xd.unset(x, y); } } else if (!xd.one_y_to_color.empty()) { auto [y, col] = *xd.one_y_to_color.begin(); moves.emplace_back('K', y, col); for (int x = 0; x < n; ++x) { xd.unset(x, y); } } else { break; } // auto [type, i, col] = moves.back(); // std::cerr << type << " " << i + 1 << " " << (char)('A' + col) << "\n"; } std::reverse(moves.begin(), moves.end()); std::cout << moves.size() << "\n"; for (auto [type, i, col]: moves) { std::cout << type << " " << i + 1 << " " << (char)('A' + col) << "\n"; } } |