#include <cassert> #include <iostream> #include <istream> #include <fstream> #include <algorithm> #include <cstdio> #include <complex> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> #include <string> #include <cstdlib> #include <memory> #include <ctime> #include <bitset> #include <queue> #include <stack> #include <unordered_map> #include <unordered_set> #include <bitset> #include <ranges> using namespace std; class Lam2024 { int n, m; vector<vector<char>> matrix; vector<string> solutions; public: int findRowWithSameChars(const std::vector<std::vector<char>>& matrix) { if (matrix.empty()) { return -1; // Return -1 if the matrix is empty } int numRows = matrix.size(); int numCols = matrix[0].size(); for (int row = 0; row < numRows; ++row) { char firstChar = matrix[row][0]; // Assume first character of the row bool sameChars = true; for (int col = 1; col < numCols; ++col) { if (matrix[row][col] != firstChar) { sameChars = false; break; // Exit the inner loop if any character in the row is different } } if (sameChars) { return row; // Return the index of the row if all characters are the same } } return -1; // Return -1 if no row has all the same characters } int findColumnWithSameChars(const std::vector<std::vector<char>>& matrix) { if (matrix.empty()) { return -1; // Return -1 if the matrix is empty } int numRows = matrix.size(); int numCols = matrix[0].size(); for (int col = 0; col < numCols; ++col) { char firstChar = matrix[0][col]; bool sameChars = true; for (int row = 1; row < numRows; ++row) { if (matrix[row][col] != firstChar) { sameChars = false; break; // Exit the inner loop if any character in the column is different } } if (sameChars) { return col; // Return the index of the column if all characters are the same } } return -1; // Return -1 if no column has all the same characters } void removeRow(std::vector<std::vector<char>>& matrix, int rowIndex) { if (rowIndex >= 0 && rowIndex < matrix.size()) { matrix.erase(matrix.begin() + rowIndex); // Erase the row at the specified index } } void removeColumn(std::vector<std::vector<char>>& matrix, int columnIndex) { for (auto& row : matrix) { if (columnIndex >= 0 && columnIndex < row.size()) { row.erase(row.begin() + columnIndex); // Erase the element at the specified column index } } } void virtual Solve() { cin >> n >> m; int rows = n; int cols = m; for (int i = 0; i < rows; i++) { vector<char> row; string str; cin >> str; for (char c : str) { row.push_back(c); } matrix.push_back(row); } vector<int> rowNumbers; for (int i = 0; i < n; i++) { rowNumbers.push_back(i + 1); } vector<int> columnsNumbers; for (int i = 0; i < m; i++) { columnsNumbers.push_back(i + 1); } while (rows > 0 && cols > 0) { int row = this->findRowWithSameChars(this->matrix); if (row >= 0) { string entry = "R " + to_string((rowNumbers.at(row))) + " " + matrix[row][0]; solutions.push_back(entry); removeRow(matrix, row); rowNumbers.erase(rowNumbers.begin() + row); rows--; } else { int col = this->findColumnWithSameChars(this->matrix); string entry = "K " + to_string((columnsNumbers.at(col))) + " " + matrix[0][col]; solutions.push_back(entry); removeColumn(matrix, col); columnsNumbers.erase(columnsNumbers.begin() + col); cols--; } } cout << solutions.size() << endl; for (int i = solutions.size() - 1; i >= 0; i--) { cout << solutions[i] << endl; } //cout << endl; } }; int main() { Lam2024* p = new Lam2024(); p->Solve(); delete p; 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | #include <cassert> #include <iostream> #include <istream> #include <fstream> #include <algorithm> #include <cstdio> #include <complex> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> #include <string> #include <cstdlib> #include <memory> #include <ctime> #include <bitset> #include <queue> #include <stack> #include <unordered_map> #include <unordered_set> #include <bitset> #include <ranges> using namespace std; class Lam2024 { int n, m; vector<vector<char>> matrix; vector<string> solutions; public: int findRowWithSameChars(const std::vector<std::vector<char>>& matrix) { if (matrix.empty()) { return -1; // Return -1 if the matrix is empty } int numRows = matrix.size(); int numCols = matrix[0].size(); for (int row = 0; row < numRows; ++row) { char firstChar = matrix[row][0]; // Assume first character of the row bool sameChars = true; for (int col = 1; col < numCols; ++col) { if (matrix[row][col] != firstChar) { sameChars = false; break; // Exit the inner loop if any character in the row is different } } if (sameChars) { return row; // Return the index of the row if all characters are the same } } return -1; // Return -1 if no row has all the same characters } int findColumnWithSameChars(const std::vector<std::vector<char>>& matrix) { if (matrix.empty()) { return -1; // Return -1 if the matrix is empty } int numRows = matrix.size(); int numCols = matrix[0].size(); for (int col = 0; col < numCols; ++col) { char firstChar = matrix[0][col]; bool sameChars = true; for (int row = 1; row < numRows; ++row) { if (matrix[row][col] != firstChar) { sameChars = false; break; // Exit the inner loop if any character in the column is different } } if (sameChars) { return col; // Return the index of the column if all characters are the same } } return -1; // Return -1 if no column has all the same characters } void removeRow(std::vector<std::vector<char>>& matrix, int rowIndex) { if (rowIndex >= 0 && rowIndex < matrix.size()) { matrix.erase(matrix.begin() + rowIndex); // Erase the row at the specified index } } void removeColumn(std::vector<std::vector<char>>& matrix, int columnIndex) { for (auto& row : matrix) { if (columnIndex >= 0 && columnIndex < row.size()) { row.erase(row.begin() + columnIndex); // Erase the element at the specified column index } } } void virtual Solve() { cin >> n >> m; int rows = n; int cols = m; for (int i = 0; i < rows; i++) { vector<char> row; string str; cin >> str; for (char c : str) { row.push_back(c); } matrix.push_back(row); } vector<int> rowNumbers; for (int i = 0; i < n; i++) { rowNumbers.push_back(i + 1); } vector<int> columnsNumbers; for (int i = 0; i < m; i++) { columnsNumbers.push_back(i + 1); } while (rows > 0 && cols > 0) { int row = this->findRowWithSameChars(this->matrix); if (row >= 0) { string entry = "R " + to_string((rowNumbers.at(row))) + " " + matrix[row][0]; solutions.push_back(entry); removeRow(matrix, row); rowNumbers.erase(rowNumbers.begin() + row); rows--; } else { int col = this->findColumnWithSameChars(this->matrix); string entry = "K " + to_string((columnsNumbers.at(col))) + " " + matrix[0][col]; solutions.push_back(entry); removeColumn(matrix, col); columnsNumbers.erase(columnsNumbers.begin() + col); cols--; } } cout << solutions.size() << endl; for (int i = solutions.size() - 1; i >= 0; i--) { cout << solutions[i] << endl; } //cout << endl; } }; int main() { Lam2024* p = new Lam2024(); p->Solve(); delete p; return 0; } |