/* Topcoder SRM 717, D2 250-Pointer. NiceTable James S. Plank Wed Jul 12 22:29:59 EDT 2017 */ #include #include #include #include #include using namespace std; class NiceTable { public: string isNice(vector t); }; string NiceTable::isNice(vector t) { int x, y, r, c, ok, a, b; /* Enumerate all possible values for x with a power set enumeration. */ for (x = 0; x < (1 << t.size()); x++) { /* Enumerate all possible values for y with a power set enumeration. */ for (y = 0; y < (1 << t[0].size()); y++) { /* Now, for each row and column element, calculate what t[r][c] should be. If t[r][c] doesn't match any of these, set ok to false. If, after you test all row and column elements, ok is true, then t matched, and you return "Nice." Otherwise, continue the enumerations. */ ok = 1; for (r = 0; r < t.size(); r++) { a = (x & (1 << r)) ? 1 : 0; for (c = 0; c < t[0].size(); c++) { b = (y & (1 << c)) ? 1 : 0; if (t[r][c] != '0' + (a ^ b)) ok = 0; } } if (ok) return "Nice"; } } /* If you are done and still haven't found a match for t, return "Not nice". */ return "Not nice"; }