#include <vector> #include <iostream> #include <unordered_map> #include <algorithm> struct MatrixMove { char direction; int id; char color; }; struct ColorCount { int id; int colorCount; }; int main() { int n, m; std::cin >> n >> m; std::vector<char> columns(m + 1, 0); std::vector<std::vector<char>> desiredMatrix(n + 1, columns); std::vector<ColorCount> maxRows; std::vector<ColorCount> maxCols; std::vector<std::unordered_map<char, int>> colorCountsInCols(m + 1, std::unordered_map<char, int>()); for (int i = 1; i <= n; i++) { std::unordered_map<char, int> colorCountsInRow; for (int j = 1; j <= m; j++) { char c; std::cin >> c; desiredMatrix[i][j] = c; colorCountsInRow[c]++; colorCountsInCols[j][c]++; } int maxCount{0}; for (auto& entry : colorCountsInRow) { maxCount = std::max(maxCount, entry.second); } ColorCount countForRow{i, maxCount}; maxRows.push_back(countForRow); } for (int j = 1; j <= m; j++) { int maxCount{0}; for (auto& entry : colorCountsInCols[j]) { maxCount = std::max(maxCount, entry.second); } ColorCount countForCol{j, maxCount}; maxCols.push_back(countForCol); } std::sort(maxRows.begin(), maxRows.end(), [](auto & left, auto & right) { return left.colorCount > right.colorCount; }); std::sort(maxCols.begin(), maxCols.end(), [](auto & left, auto & right) { return left.colorCount > right.colorCount; }); std::vector<int> availableRows; for (int i = 1; i <= n; i++) { availableRows.push_back(i); } std::vector<int> availableColumns; for (int j = 1; j <= m; j++) { availableColumns.push_back(j); } std::vector<MatrixMove> moves; bool changed{true}; while (changed) { changed = false; for (auto itRow = maxRows.begin(); itRow != maxRows.end();) { char commonColor{0}; for (auto itCol = availableColumns.begin(); itCol != availableColumns.end(); itCol++) { if (commonColor == 0) { commonColor = desiredMatrix[itRow->id][*itCol]; } else if (desiredMatrix[itRow->id][*itCol] != commonColor) { commonColor = 0; break; } } if (commonColor != 0) { MatrixMove nextMove; nextMove.direction = 'R'; nextMove.id = itRow->id; nextMove.color = commonColor; moves.push_back(nextMove); availableRows.erase(std::find(availableRows.begin(), availableRows.end(), itRow->id)); itRow = maxRows.erase(itRow); changed = true; } else { itRow++; } } for (auto itCol = maxCols.begin(); itCol != maxCols.end();) { char commonColor{0}; for (auto itRow = availableRows.begin(); itRow != availableRows.end(); itRow++) { if (commonColor == 0) { commonColor = desiredMatrix[*itRow][itCol->id]; } else if(desiredMatrix[*itRow][itCol->id] != commonColor) { commonColor = 0; break; } } if (commonColor != 0) { MatrixMove nextMove; nextMove.direction = 'K'; nextMove.id = itCol->id; nextMove.color = commonColor; moves.push_back(nextMove); availableColumns.erase(std::find(availableColumns.begin(), availableColumns.end(), itCol->id)); itCol = maxCols.erase(itCol); changed = true; break; } else { itCol++; } } } std::cout << moves.size() << "\n"; for (auto it = moves.rbegin(); it != moves.rend(); it++) { std::cout << it->direction << " " << it->id << " " << it->color << "\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 120 121 122 123 124 125 126 127 128 | #include <vector> #include <iostream> #include <unordered_map> #include <algorithm> struct MatrixMove { char direction; int id; char color; }; struct ColorCount { int id; int colorCount; }; int main() { int n, m; std::cin >> n >> m; std::vector<char> columns(m + 1, 0); std::vector<std::vector<char>> desiredMatrix(n + 1, columns); std::vector<ColorCount> maxRows; std::vector<ColorCount> maxCols; std::vector<std::unordered_map<char, int>> colorCountsInCols(m + 1, std::unordered_map<char, int>()); for (int i = 1; i <= n; i++) { std::unordered_map<char, int> colorCountsInRow; for (int j = 1; j <= m; j++) { char c; std::cin >> c; desiredMatrix[i][j] = c; colorCountsInRow[c]++; colorCountsInCols[j][c]++; } int maxCount{0}; for (auto& entry : colorCountsInRow) { maxCount = std::max(maxCount, entry.second); } ColorCount countForRow{i, maxCount}; maxRows.push_back(countForRow); } for (int j = 1; j <= m; j++) { int maxCount{0}; for (auto& entry : colorCountsInCols[j]) { maxCount = std::max(maxCount, entry.second); } ColorCount countForCol{j, maxCount}; maxCols.push_back(countForCol); } std::sort(maxRows.begin(), maxRows.end(), [](auto & left, auto & right) { return left.colorCount > right.colorCount; }); std::sort(maxCols.begin(), maxCols.end(), [](auto & left, auto & right) { return left.colorCount > right.colorCount; }); std::vector<int> availableRows; for (int i = 1; i <= n; i++) { availableRows.push_back(i); } std::vector<int> availableColumns; for (int j = 1; j <= m; j++) { availableColumns.push_back(j); } std::vector<MatrixMove> moves; bool changed{true}; while (changed) { changed = false; for (auto itRow = maxRows.begin(); itRow != maxRows.end();) { char commonColor{0}; for (auto itCol = availableColumns.begin(); itCol != availableColumns.end(); itCol++) { if (commonColor == 0) { commonColor = desiredMatrix[itRow->id][*itCol]; } else if (desiredMatrix[itRow->id][*itCol] != commonColor) { commonColor = 0; break; } } if (commonColor != 0) { MatrixMove nextMove; nextMove.direction = 'R'; nextMove.id = itRow->id; nextMove.color = commonColor; moves.push_back(nextMove); availableRows.erase(std::find(availableRows.begin(), availableRows.end(), itRow->id)); itRow = maxRows.erase(itRow); changed = true; } else { itRow++; } } for (auto itCol = maxCols.begin(); itCol != maxCols.end();) { char commonColor{0}; for (auto itRow = availableRows.begin(); itRow != availableRows.end(); itRow++) { if (commonColor == 0) { commonColor = desiredMatrix[*itRow][itCol->id]; } else if(desiredMatrix[*itRow][itCol->id] != commonColor) { commonColor = 0; break; } } if (commonColor != 0) { MatrixMove nextMove; nextMove.direction = 'K'; nextMove.id = itCol->id; nextMove.color = commonColor; moves.push_back(nextMove); availableColumns.erase(std::find(availableColumns.begin(), availableColumns.end(), itCol->id)); itCol = maxCols.erase(itCol); changed = true; break; } else { itCol++; } } } std::cout << moves.size() << "\n"; for (auto it = moves.rbegin(); it != moves.rend(); it++) { std::cout << it->direction << " " << it->id << " " << it->color << "\n"; } return 0; } |