https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2998
就是简单的一个暴力深搜 代码写屎了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
int date[25][25][90];
int n;
string s;
bool checkColor(int x,int y,int c,int d)
{
if(x>2){
if(date[x-2][y-1][d]==c) return 0;
if(date[x-2][y][d]==c) return 0;
if(date[x-2][y+1][d]==c) return 0;
}
if(x<=n-3)
{
if(date[x+2][y-1][d]==c) return 0;
if(date[x+2][y][d]==c) return 0;
if(date[x+2][y+1][d]==c) return 0;
}
if(y>=2)
{
if(date[x-1][y-2][d]==c) return 0;
if(date[x][y-2][d]==c) return 0;
if(date[x+1][y-2][d]==c) return 0;
}
if(y<=n-3)
{
if(date[x-1][y+2][d]==c) return 0;
if(date[x+1][y+2][d]==c) return 0;
if(date[x][y+2][d]==c) return 0;
}
if(date[x-1][y-1][d]==c) return 0;
if(date[x-1][y+1][d]==c) return 0;
if(date[x+1][y-1][d]==c) return 0;
if(date[x+1][y+1][d]==c) return 0;
else return 1;
}
bool check (int x,int y,int dp)
{
if(date[x-1][y][dp]==0&&date[x+1][y][dp]==0&&date[x][y-1][dp]==0&&date[x][y+1][dp]==0&&date[x][y][dp]==0)
{
return true;
}
else return false;
}
void init(int d)
{
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
date[i][j][d]=date[i][j][d-1];
}
}
}
void print(int d)
{
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
printf("%d",date[i][j][d]);
}puts("");
}
printf("%d",d);
puts("");
}
bool dfs(int x,int y,int d,int c)
{
init(d);
date[x-1][y][d]=c;
date[x+1][y][d]=c;
date[x][y+1][d]=c;
date[x][y-1][d]=c;
date[x][y][d]=c;
//print(d);
for (int j=x;j<n-1;j++)
{
for (int i=1;i<n-1;i++)
{
if(check(j,i,d))
{
bool bcolor2,bcolor3,bcolor4;
bcolor2=checkColor(j,i,2,d);
bcolor3=checkColor(j,i,3,d);
bcolor4=checkColor(j,i,4,d);
if (bcolor2 && dfs(j, i, d+1, 2)) return true;
if (bcolor3 && dfs(j, i, d+1, 3)) return true;
if (bcolor4 && dfs(j, i, d+1, 4)) return true;
return false;
}
}
}
bool empty=0;
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(date[i][j][d]==0)
{
empty=1;
break;
}
}
if(empty) return false;
}
return true;
}
int main ()
{
//freopen ("data.in","r",stdin);
int T;
int cas=0;
scanf("%d",&T);
while (T--)
{
printf("Case %d:",++cas);
memset(date,0,sizeof(date));
scanf("%d",&n);
int cnt=0;
for (int i=0;i<n;i++)
{
cin>>s;
for (int j=0;j<n;j++)
{
if(s[j]=='A') date[i][j][0]=1;
else {
date[i][j][0]=0;
cnt++;
}
}
}
bool haveStart=0;
for (int i=1;i<n-1;i++)
{
for (int j=1;j<n-1;j++)
{
if(check(i,j,0))
{
dfs(i,j,1,2);
haveStart=1;
break;
}
}
if(haveStart) break;
}
if(cnt%5) {
puts(" Not Possible!");
continue;
}
cnt=cnt/5;
bool empty=0;
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(date[i][j][cnt]==0)
{
empty=1;
break;
}
}
if(empty) break;
}
if(empty){
puts(" Not Possible!");
continue;
}
puts("");
char temp;
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
temp=date[i][j][cnt]-1+'A';
printf("%c",temp);
}
puts("");
}
}
return 0;
}