abc157_b - Bingo

Check horizontally, vertically and in both diagonals.

Time complexity: $O(n)$

Memory complexity: $O(1)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    array<array<int, 3>, 3> bingo;
    for (auto &row : bingo)
        for (auto &val : row)
            cin >> val;
    int n;
    cin >> n;
    while (n--)
    {
        int bi;
        cin >> bi;
        for (auto &row : bingo)
            for (auto &val : row)
                if (val == bi)
                    val = 0;
    }
    vi ver_cnt(3, 0), hor_cnt(3, 0), diag_cnt(2, 0);
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 3; ++j)
            if (bingo[i][j] == 0)
                ver_cnt[j]++, hor_cnt[i]++;
    for (int i = 0; i < 3; ++i)
    {
        if (bingo[i][i] == 0)
            diag_cnt[0]++;
        if (bingo[i][3 - i - 1] == 0)
            diag_cnt[1]++;
    }
    bool ans = any_of(begin(ver_cnt), end(ver_cnt), [](int cnt) { return cnt == 3; }) |
               any_of(begin(hor_cnt), end(hor_cnt), [](int cnt) { return cnt == 3; }) |
               any_of(begin(diag_cnt), end(diag_cnt), [](int cnt) { return cnt == 3; });
    cout << (ans ? "Yes" : "No") << endl;
    return 0;
}