#include <iostream> #include <algorithm> #include <vector> #include <string> #include <array> #include <tuple> int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); int h, w; std::cin >> h >> w; std::vector<std::vector<char>> board(w, std::vector<char>(h)); std::string str; for (int y = 0; y < h; ++y) { std::cin >> str; for (int x = 0; x < w; ++x) { board[x][y] = str[x]; } } constexpr int ALSZ = 'Z' - 'A' + 1; std::vector<std::array<int, ALSZ>> col_cnts(w); std::vector<std::array<int, ALSZ>> row_cnts(h); auto good = [](const std::array<int, ALSZ> &cnts) { int idx = -1; for (int i = 0; i < ALSZ; ++i) { if (cnts[i]) { if (idx >= 0) return -1; idx = i; } } return idx; }; for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { col_cnts[i][board[i][j] - 'A']++; row_cnts[j][board[i][j] - 'A']++; } } for (int i = 0; i < w; ++i) { int idx = good(col_cnts[i]); if (idx < 0) continue; } std::vector<int> xs(w); std::vector<int> ys(h); for (int i = 0; i < w; ++i) xs[i] = i; for (int i = 0; i < h; ++i) ys[i] = i; // [(row?, coordinate, color)] std::vector<std::tuple<bool,int,int>> ops; int nxs = xs.size(); int nys = ys.size(); while (nxs > 0 && nys > 0) { int p; p = 0; for (int i = 0; i < nxs; ++i) { xs[p] = xs[i]; int idx = good(col_cnts[xs[i]]); if (idx >= 0) { ops.emplace_back(false, xs[i], idx); for (int j = 0; j < nys; ++j) { row_cnts[ys[j]][idx]--; } } else { ++p; } } nxs = p; xs.resize(nxs); p = 0; for (int i = 0; i < nys; ++i) { ys[p] = ys[i]; int idx = good(row_cnts[ys[i]]); if (idx >= 0) { ops.emplace_back(true, ys[i], idx); for (int j = 0; j < nxs; ++j) { col_cnts[xs[j]][idx]--; } } else { ++p; } } nys = p; ys.resize(nys); } std::reverse(ops.begin(), ops.end()); std::cout << ops.size() << '\n'; for (auto [isrow, coord, color] : ops) { std::cout << (isrow ? 'R' : 'K') << ' ' << (coord + 1) << ' ' << char('A' + color) << '\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 | #include <iostream> #include <algorithm> #include <vector> #include <string> #include <array> #include <tuple> int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); int h, w; std::cin >> h >> w; std::vector<std::vector<char>> board(w, std::vector<char>(h)); std::string str; for (int y = 0; y < h; ++y) { std::cin >> str; for (int x = 0; x < w; ++x) { board[x][y] = str[x]; } } constexpr int ALSZ = 'Z' - 'A' + 1; std::vector<std::array<int, ALSZ>> col_cnts(w); std::vector<std::array<int, ALSZ>> row_cnts(h); auto good = [](const std::array<int, ALSZ> &cnts) { int idx = -1; for (int i = 0; i < ALSZ; ++i) { if (cnts[i]) { if (idx >= 0) return -1; idx = i; } } return idx; }; for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { col_cnts[i][board[i][j] - 'A']++; row_cnts[j][board[i][j] - 'A']++; } } for (int i = 0; i < w; ++i) { int idx = good(col_cnts[i]); if (idx < 0) continue; } std::vector<int> xs(w); std::vector<int> ys(h); for (int i = 0; i < w; ++i) xs[i] = i; for (int i = 0; i < h; ++i) ys[i] = i; // [(row?, coordinate, color)] std::vector<std::tuple<bool,int,int>> ops; int nxs = xs.size(); int nys = ys.size(); while (nxs > 0 && nys > 0) { int p; p = 0; for (int i = 0; i < nxs; ++i) { xs[p] = xs[i]; int idx = good(col_cnts[xs[i]]); if (idx >= 0) { ops.emplace_back(false, xs[i], idx); for (int j = 0; j < nys; ++j) { row_cnts[ys[j]][idx]--; } } else { ++p; } } nxs = p; xs.resize(nxs); p = 0; for (int i = 0; i < nys; ++i) { ys[p] = ys[i]; int idx = good(row_cnts[ys[i]]); if (idx >= 0) { ops.emplace_back(true, ys[i], idx); for (int j = 0; j < nxs; ++j) { col_cnts[xs[j]][idx]--; } } else { ++p; } } nys = p; ys.resize(nys); } std::reverse(ops.begin(), ops.end()); std::cout << ops.size() << '\n'; for (auto [isrow, coord, color] : ops) { std::cout << (isrow ? 'R' : 'K') << ' ' << (coord + 1) << ' ' << char('A' + color) << '\n'; } } |