方格涂色

方格涂色

AcWing 3711
https://www.acwing.com/problem/content/3714/

枚举题 枚举四个角的选择方案 2^4=16种

对于任意一条边,除两个端点外,中间n-2个格子是完全独立的。
我们用a、b、x、y来代表四个点是否被染色,1表示染色,0表示未染色。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int n;
int u, r, d, l;

bool check(int state)
{
    int a = state >> 0 & 1, b = state >> 1 & 1;
    int x = state >> 2 & 1, y = state >> 3 & 1;
    if (!(a + b <= u && u <= a + b + n - 2)) return false;
    if (!(x + y <= d && d <= x + y + n - 2)) return false;
    if (!(a + x <= l && l <= a + x + n - 2)) return false;
    if (!(b + y <= r && r <= b + y + n - 2)) return false;
    return true;
}

int main()
{
    int T;
    cin >> T;
    while (T -- )
    {
        cin >> n >> u >> r >> d >> l;
        bool flag = false;
        for (int i = 0; i < 16; i ++ )
            if (check(i))
            {
                flag = true;
                break;
            }
        if (flag) puts("YES");
        else puts("NO");
    }

    return 0;
}
posted @ 2021-06-22 20:50  Treasure_lee  阅读(395)  评论(0)    收藏  举报