51nod 1448

倒着考虑印的过程,最后一次印的区域一定是K*K的同色区域
然后在之前的操作这片区域里印成什么颜色都行,定义为杂色,每次找一个K*K的全0/1+杂色区域印,(nm)^6

code:

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;

const int maxn = 21;

int n,K;
char str[maxn];
int col[maxn][maxn];

bool check(const int i,const int j,const int w,const int h)
{
    int sx=i-w+1,sy=j-h+1,cc=col[i][j];
    if(sx<=0||sy<=0||sx+K-1>n||sy+K-1>n) return false;
    for(int x=sx;x<=sx+K-1;x++)
    {
        for(int y=sy;y<=sy+K-1;y++) if(col[x][y]!=-1)
            if(col[x][y]!=cc) return false;
    }
    return true;
}
int num;
void mark(const int x,const int y)
{
    for(int i=x;i<=x+K-1;i++) for(int j=y;j<=y+K-1;j++) if(col[i][j]!=-1)
        col[i][j]=-1,num--;
}

int main()
{
    int tcase; scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d%d",&n,&K);
        for(int i=1;i<=n;i++) 
        {
            scanf("%s",str);
            for(int j=1;j<=n;j++) col[i][j]=str[j-1]=='W'?0:1;
        }
        num=n*n;
        while(num)
        {
            int tx=-1,ty;
            for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(col[i][j]!=-1)
            {
                for(int w=1;w<=K;w++) 
                {
                    for(int h=1;h<=K;h++) if(check(i,j,w,h)) { tx=i,ty=j,mark(i-w+1,j-h+1); break; }
                    if(tx!=-1) break;
                }
                if(tx!=-1) break;
            }
            if(tx==-1) break;
        }
        if(!num) puts("Possible");
        else puts("Impossible");
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值