#include <bits/stdc++.h> using namespace std; typedef long long ll; #if DEBUG #define LOG(...) printf(__VA_ARGS__) #define DOUT cout #else #define LOG(...) #define DOUT if(0) cout #endif bool check_row(const vector<string> &canvas, int nr, char &color) { int x = canvas[0].size(); color = '*'; bool succ = true; for (int i = 0; i < x; i++) { if (canvas[nr][i] != '*') { if (color == '*') color = canvas[nr][i]; else { if (canvas[nr][i] != color) { succ = false; break; } } } } return succ && color != '*'; } bool check_column(const vector<string> &canvas, int nr, char &color) { int y = canvas.size(); color = '*'; bool succ = true; for (int i = 0; i < y; i++) { if (canvas[i][nr] != '*') { if (color == '*') color = canvas[i][nr]; else { if (canvas[i][nr] != color) { succ = false; break; } } } } return succ && color != '*'; } struct Match { char dir, color; int nr; Match(char dir, char color, int nr): dir(dir), color(color), nr(nr) {} }; void check_all_rows( vector<string> &canvas, vector<Match> &matches, vector<bool> &skip ) { int y = canvas.size(); int x = canvas[0].size(); for (int i = 0; i < y; i++) { // if (skip[i]) // continue; char color; bool found = check_row(canvas, i, color); if (found) { matches.emplace_back('R', color, i + 1); // skip[i] = true; DOUT << "row " << i + 1 << " " << color << "\n"; for (int j = 0; j < x; j++) canvas[i][j] = '*'; } } } void check_all_columns( vector<string> &canvas, vector<Match> &matches, vector<bool> &skip ) { int y = canvas.size(); int x = canvas[0].size(); for (int i = 0; i < x; i++) { // if (skip[i]) // continue; char color; bool found = check_column(canvas, i, color); if (found) { matches.emplace_back('K', color, i + 1); // skip[i] = true; DOUT << "column " << i + 1 << " " << color << "\n"; for (int j = 0; j < y; j++) canvas[j][i] = '*'; } } } void test_check_row(); void test_check_column(); void test_check_all_rows(); void test_check_all_columns(); int main() { #if DEBUG test_check_row(); test_check_column(); test_check_all_rows(); test_check_all_columns(); #else ios_base::sync_with_stdio(false); #endif int y, x; cin >> y >> x; vector<string> canvas(y); for (int i = 0; i < y; i++) cin >> canvas[i]; vector<Match> matches; bool found_anything; vector<bool> skip_columns(x, false); vector<bool> skip_rows(y, false); do { found_anything = false; int before = matches.size(); check_all_columns(canvas, matches, skip_columns); int after = matches.size(); if (after > before) found_anything = true; DOUT << "after checking columns\n"; for (int i = before; i < after; i++) DOUT << matches[i].dir << " " << matches[i].nr << " " << matches[i].color << "\n"; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) DOUT << canvas[i][j]; DOUT << "\n"; } before = matches.size(); check_all_rows(canvas, matches, skip_rows); after = matches.size(); if (after > before) found_anything = true; DOUT << "after checking rows\n"; for (int i = before; i < after; i++) DOUT << matches[i].dir << " " << matches[i].nr << " " << matches[i].color << "\n"; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) DOUT << canvas[i][j]; DOUT << "\n"; } } while (found_anything); reverse(matches.begin(), matches.end()); cout << matches.size() << "\n"; for (const Match &m : matches) cout << m.dir << " " << m.nr << " " << m .color << "\n"; } void test_check_row() { vector<string> canvas = { "AAA*", "A*B*", "****", "B*B*" }; // check row 0 char color; bool found = check_row(canvas, 0, color); assert(found); assert(color == 'A'); // check row 1 found = check_row(canvas, 1, color); assert(!found); // check row 2 found = check_row(canvas, 2, color); assert(!found); // check row 3 found = check_row(canvas, 3, color); assert(found); assert(color == 'B'); DOUT << "test_check_row passed\n"; } void test_check_column() { vector<string> canvas = { "AAA*", "A*B*", "****", "B*B*" }; bool found; char color; // check column 1 found = check_column(canvas, 0, color); assert(!found); // check column 2 found = check_column(canvas, 1, color); assert(found); assert(color == 'A'); // check column 3 found = check_column(canvas, 2, color); assert(!found); // check column 4 found = check_column(canvas, 3, color); assert(!found); DOUT << "test_check_column passed\n"; } void test_check_all_columns() { vector<string> canvas = { "AAA*", "A*B*", "****", "AAA*" }; vector<Match> matches; vector<bool> skip(4, false); check_all_columns(canvas, matches, skip); // K 1 A assert(canvas[0][0] == '*'); assert(canvas[1][0] == '*'); assert(canvas[2][0] == '*'); assert(canvas[3][0] == '*'); // K 2 A assert(canvas[0][1] == '*'); assert(canvas[1][1] == '*'); assert(canvas[2][1] == '*'); assert(canvas[3][1] == '*'); assert((int)matches.size() == 2); DOUT << "test_check_all_columns passed\n"; } void test_check_all_rows() { vector<string> canvas = { "A*AA", "A*BA", "****", "AAAA" }; vector<Match> matches; vector<bool> skip(4, false); check_all_rows(canvas, matches, skip); // R 1 A assert(canvas[0][0] == '*'); assert(canvas[0][1] == '*'); assert(canvas[0][2] == '*'); assert(canvas[0][3] == '*'); // R 3 A assert(canvas[3][0] == '*'); assert(canvas[3][1] == '*'); assert(canvas[3][2] == '*'); assert(canvas[3][3] == '*'); assert((int)matches.size() == 2); DOUT << "test_check_all_rows passed\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 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #if DEBUG #define LOG(...) printf(__VA_ARGS__) #define DOUT cout #else #define LOG(...) #define DOUT if(0) cout #endif bool check_row(const vector<string> &canvas, int nr, char &color) { int x = canvas[0].size(); color = '*'; bool succ = true; for (int i = 0; i < x; i++) { if (canvas[nr][i] != '*') { if (color == '*') color = canvas[nr][i]; else { if (canvas[nr][i] != color) { succ = false; break; } } } } return succ && color != '*'; } bool check_column(const vector<string> &canvas, int nr, char &color) { int y = canvas.size(); color = '*'; bool succ = true; for (int i = 0; i < y; i++) { if (canvas[i][nr] != '*') { if (color == '*') color = canvas[i][nr]; else { if (canvas[i][nr] != color) { succ = false; break; } } } } return succ && color != '*'; } struct Match { char dir, color; int nr; Match(char dir, char color, int nr): dir(dir), color(color), nr(nr) {} }; void check_all_rows( vector<string> &canvas, vector<Match> &matches, vector<bool> &skip ) { int y = canvas.size(); int x = canvas[0].size(); for (int i = 0; i < y; i++) { // if (skip[i]) // continue; char color; bool found = check_row(canvas, i, color); if (found) { matches.emplace_back('R', color, i + 1); // skip[i] = true; DOUT << "row " << i + 1 << " " << color << "\n"; for (int j = 0; j < x; j++) canvas[i][j] = '*'; } } } void check_all_columns( vector<string> &canvas, vector<Match> &matches, vector<bool> &skip ) { int y = canvas.size(); int x = canvas[0].size(); for (int i = 0; i < x; i++) { // if (skip[i]) // continue; char color; bool found = check_column(canvas, i, color); if (found) { matches.emplace_back('K', color, i + 1); // skip[i] = true; DOUT << "column " << i + 1 << " " << color << "\n"; for (int j = 0; j < y; j++) canvas[j][i] = '*'; } } } void test_check_row(); void test_check_column(); void test_check_all_rows(); void test_check_all_columns(); int main() { #if DEBUG test_check_row(); test_check_column(); test_check_all_rows(); test_check_all_columns(); #else ios_base::sync_with_stdio(false); #endif int y, x; cin >> y >> x; vector<string> canvas(y); for (int i = 0; i < y; i++) cin >> canvas[i]; vector<Match> matches; bool found_anything; vector<bool> skip_columns(x, false); vector<bool> skip_rows(y, false); do { found_anything = false; int before = matches.size(); check_all_columns(canvas, matches, skip_columns); int after = matches.size(); if (after > before) found_anything = true; DOUT << "after checking columns\n"; for (int i = before; i < after; i++) DOUT << matches[i].dir << " " << matches[i].nr << " " << matches[i].color << "\n"; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) DOUT << canvas[i][j]; DOUT << "\n"; } before = matches.size(); check_all_rows(canvas, matches, skip_rows); after = matches.size(); if (after > before) found_anything = true; DOUT << "after checking rows\n"; for (int i = before; i < after; i++) DOUT << matches[i].dir << " " << matches[i].nr << " " << matches[i].color << "\n"; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) DOUT << canvas[i][j]; DOUT << "\n"; } } while (found_anything); reverse(matches.begin(), matches.end()); cout << matches.size() << "\n"; for (const Match &m : matches) cout << m.dir << " " << m.nr << " " << m .color << "\n"; } void test_check_row() { vector<string> canvas = { "AAA*", "A*B*", "****", "B*B*" }; // check row 0 char color; bool found = check_row(canvas, 0, color); assert(found); assert(color == 'A'); // check row 1 found = check_row(canvas, 1, color); assert(!found); // check row 2 found = check_row(canvas, 2, color); assert(!found); // check row 3 found = check_row(canvas, 3, color); assert(found); assert(color == 'B'); DOUT << "test_check_row passed\n"; } void test_check_column() { vector<string> canvas = { "AAA*", "A*B*", "****", "B*B*" }; bool found; char color; // check column 1 found = check_column(canvas, 0, color); assert(!found); // check column 2 found = check_column(canvas, 1, color); assert(found); assert(color == 'A'); // check column 3 found = check_column(canvas, 2, color); assert(!found); // check column 4 found = check_column(canvas, 3, color); assert(!found); DOUT << "test_check_column passed\n"; } void test_check_all_columns() { vector<string> canvas = { "AAA*", "A*B*", "****", "AAA*" }; vector<Match> matches; vector<bool> skip(4, false); check_all_columns(canvas, matches, skip); // K 1 A assert(canvas[0][0] == '*'); assert(canvas[1][0] == '*'); assert(canvas[2][0] == '*'); assert(canvas[3][0] == '*'); // K 2 A assert(canvas[0][1] == '*'); assert(canvas[1][1] == '*'); assert(canvas[2][1] == '*'); assert(canvas[3][1] == '*'); assert((int)matches.size() == 2); DOUT << "test_check_all_columns passed\n"; } void test_check_all_rows() { vector<string> canvas = { "A*AA", "A*BA", "****", "AAAA" }; vector<Match> matches; vector<bool> skip(4, false); check_all_rows(canvas, matches, skip); // R 1 A assert(canvas[0][0] == '*'); assert(canvas[0][1] == '*'); assert(canvas[0][2] == '*'); assert(canvas[0][3] == '*'); // R 3 A assert(canvas[3][0] == '*'); assert(canvas[3][1] == '*'); assert(canvas[3][2] == '*'); assert(canvas[3][3] == '*'); assert((int)matches.size() == 2); DOUT << "test_check_all_rows passed\n"; } |