// If a cell is zero, the row and column are set to zero. #define CATCH_CONFIG_MAIN #include #include #include #include #include using string_view = std::experimental::string_view; using matrix = std::array, 3>; template std::string to_string(const T& img) { std::string out; for (const auto& row : img) { for (auto ch : row) { out.push_back(ch); } out.push_back(';'); } return out; } void fill(matrix& m) { for (auto& row : m) { for (auto& ch : row) { ch = '1'; } } } void clear(matrix& m) { // Record the rows and cols that have a zero in them. std::unordered_set rows; std::unordered_set cols; for (size_t y = 0; y < m.size(); y++) { auto& row = m[y]; for (size_t x = 0; x < row.size(); x++) { if (row[x] == '0') { cols.insert(x); rows.insert(y); } } } // Now go and clear the rows and cols. for (auto y : rows) { for (auto& ch : m[y]) { ch = '0'; } } for (auto x : cols) { for (auto& row : m) { row[x] = '0'; } } } TEST_CASE("cc18", "zeros") { matrix m; fill(m); CHECK(to_string(m) == "1111;1111;1111;"); m[0][1] = '0'; CHECK(to_string(m) == "1011;1111;1111;"); clear(m); CHECK(to_string(m) == "0000;1011;1011;"); fill(m); m[0][0] = '0'; m[2][3] = '0'; clear(m); CHECK(to_string(m) == "0000;0110;0000;"); }