// Autor: Nicolas Wochnik #include <array> #include <iostream> #include <list> #include <vector> std::vector<std::vector<char>> array; struct Move { bool is_row; size_t index; char color; Move(bool is_row, size_t index, char color) : is_row(is_row), index(index), color(color) {} void print() { std::cout << (is_row ? 'R' : 'K') << " " << (index + 1) << " " << color << "\n"; } }; std::vector<Move> moves; class Row { private: std::array<long, 26> row; public: size_t index; public: Row() : index(), row() { row.fill(0); } void add_char(char c) { row[c - 'A']++; } void remove_char(char c) { row[c - 'A']--; } bool is_one_color() { bool has_color = false; for (auto count : row) { if (count == 0) { continue; } if (has_color) { return false; } has_color = true; } return true; } void debug() { std::cout << index << " ->\t"; for (auto item : row) { std::cout << item << " "; } std::cout << "\n"; } }; class Board { private: std::list<Row> rows; std::list<Row> columns; public: Board() : rows(), columns() { rows.resize(array.size()); columns.resize(array[0].size()); auto row = rows.begin(); auto col = columns.begin(); for (size_t i = 0; i < array.size(); ++i) { row->index = i; for (size_t j = 0; j < array[i].size(); ++j) { row->add_char(array[i][j]); } row++; } for (size_t j = 0; j < array[0].size(); ++j) { col->index = j; for (size_t i = 0; i < array.size(); ++i) { col->add_char(array[i][j]); } col++; } } bool empty() { return rows.size() == 0 && columns.size() == 0; } const std::list<Row>::iterator find_one_color_row() { return find_one_color(rows.begin(), rows.end()); } const std::list<Row>::iterator find_one_color_col() { return find_one_color(columns.begin(), columns.end()); } void remove_row(const std::list<Row>::iterator row) { if (row == rows.end()) { return; } if (columns.empty()) { rows.erase(row); return; } moves.push_back( Move(true, row->index, array[row->index][columns.begin()->index])); for (auto &col : columns) { col.remove_char(array[row->index][col.index]); } rows.erase(row); } void remove_col(const std::list<Row>::iterator col) { if (col == columns.end()) { return; } if (rows.empty()) { columns.erase(col); return; } moves.push_back( Move(false, col->index, array[rows.begin()->index][col->index])); for (auto &row : rows) { row.remove_char(array[row.index][col->index]); } columns.erase(col); } void debug() { std::cout << "Rows:\n"; for (auto &row : rows) { row.debug(); } std::cout << "\nColumns:\n"; for (auto &col : columns) { col.debug(); } std::cout << "\n" << std::flush; } private: const std::list<Row>::iterator find_one_color(const std::list<Row>::iterator begin, const std::list<Row>::iterator end) { auto ptr = begin; for (; ptr != end; ++ptr) { if (ptr->is_one_color()) { return ptr; } } return ptr; } } *board; void load_input() { size_t height, width; std::cin >> height >> width; array.resize(height); for (auto &row : array) { row.resize(width); for (char &item : row) { std::cin >> item; } } board = new Board(); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); load_input(); while (!board->empty()) { board->remove_row(board->find_one_color_row()); board->remove_col(board->find_one_color_col()); } std::cout << moves.size() << "\n"; for (size_t i = moves.size(); i-- != 0;) { moves[i].print(); } 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | // Autor: Nicolas Wochnik #include <array> #include <iostream> #include <list> #include <vector> std::vector<std::vector<char>> array; struct Move { bool is_row; size_t index; char color; Move(bool is_row, size_t index, char color) : is_row(is_row), index(index), color(color) {} void print() { std::cout << (is_row ? 'R' : 'K') << " " << (index + 1) << " " << color << "\n"; } }; std::vector<Move> moves; class Row { private: std::array<long, 26> row; public: size_t index; public: Row() : index(), row() { row.fill(0); } void add_char(char c) { row[c - 'A']++; } void remove_char(char c) { row[c - 'A']--; } bool is_one_color() { bool has_color = false; for (auto count : row) { if (count == 0) { continue; } if (has_color) { return false; } has_color = true; } return true; } void debug() { std::cout << index << " ->\t"; for (auto item : row) { std::cout << item << " "; } std::cout << "\n"; } }; class Board { private: std::list<Row> rows; std::list<Row> columns; public: Board() : rows(), columns() { rows.resize(array.size()); columns.resize(array[0].size()); auto row = rows.begin(); auto col = columns.begin(); for (size_t i = 0; i < array.size(); ++i) { row->index = i; for (size_t j = 0; j < array[i].size(); ++j) { row->add_char(array[i][j]); } row++; } for (size_t j = 0; j < array[0].size(); ++j) { col->index = j; for (size_t i = 0; i < array.size(); ++i) { col->add_char(array[i][j]); } col++; } } bool empty() { return rows.size() == 0 && columns.size() == 0; } const std::list<Row>::iterator find_one_color_row() { return find_one_color(rows.begin(), rows.end()); } const std::list<Row>::iterator find_one_color_col() { return find_one_color(columns.begin(), columns.end()); } void remove_row(const std::list<Row>::iterator row) { if (row == rows.end()) { return; } if (columns.empty()) { rows.erase(row); return; } moves.push_back( Move(true, row->index, array[row->index][columns.begin()->index])); for (auto &col : columns) { col.remove_char(array[row->index][col.index]); } rows.erase(row); } void remove_col(const std::list<Row>::iterator col) { if (col == columns.end()) { return; } if (rows.empty()) { columns.erase(col); return; } moves.push_back( Move(false, col->index, array[rows.begin()->index][col->index])); for (auto &row : rows) { row.remove_char(array[row.index][col->index]); } columns.erase(col); } void debug() { std::cout << "Rows:\n"; for (auto &row : rows) { row.debug(); } std::cout << "\nColumns:\n"; for (auto &col : columns) { col.debug(); } std::cout << "\n" << std::flush; } private: const std::list<Row>::iterator find_one_color(const std::list<Row>::iterator begin, const std::list<Row>::iterator end) { auto ptr = begin; for (; ptr != end; ++ptr) { if (ptr->is_one_color()) { return ptr; } } return ptr; } } *board; void load_input() { size_t height, width; std::cin >> height >> width; array.resize(height); for (auto &row : array) { row.resize(width); for (char &item : row) { std::cin >> item; } } board = new Board(); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); load_input(); while (!board->empty()) { board->remove_row(board->find_one_color_row()); board->remove_col(board->find_one_color_col()); } std::cout << moves.size() << "\n"; for (size_t i = moves.size(); i-- != 0;) { moves[i].print(); } return 0; } |