倒着考虑印的过程,最后一次印的区域一定是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;
}