思路:4X4的,直接爆搜就好了
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5;
char a[maxn][maxn];
int rec[20][2], cnt;
bool dfs(int th)
{
//cout<<"th:"<<th<<endl;
if(th>=cnt) return true;
int x = rec[th][0], y=rec[th][1];
int vis[5];
memset(vis, 0, sizeof(vis));
for(int i=1;i<=4;++i)
{
if(a[x][i]!='*') vis[a[x][i]-'0']=1;
if(a[i][y]!='*') vis[a[i][y]-'0']=1;
}
if(x<=2)
{
if(y<=2)
{
for(int i=1;i<=2;++i)
for(int j=1;j<=2;++j) if(a[i][j]!='*') vis[a[i][j]-'0']=1;
}
else
{
for(int i=1;i<=2;++i)
for(int j=3;j<=4;++j) if(a[i][j]!='*') vis[a[i][j]-'0']=1;
}
}
else
{
if(y<=2)
{
for(int i=3;i<=4;++i)
for(int j=1;j<=2;++j) if(a[i][j]!='*') vis[a[i][j]-'0']=1;
}
else
{
for(int i=3;i<=4;++i)
for(int j=3;j<=4;++j) if(a[i][j]!='*') vis[a[i][j]-'0']=1;
}
}
for(int i=1;i<=4;++i)
{
if(!vis[i])
{
a[rec[th][0]][rec[th][1]] = i+'0';
if(dfs(th+1)) return true;
a[rec[th][0]][rec[th][1]] = '*';
}
}
return false;
}
int main()
{
int ca, tt=1;
scanf("%d", &ca);
while(ca--)
{
for(int i=1;i<=4;++i) scanf("%s", a[i]+1);
int x=1, y=1;
cnt=0;
for(int i=1;i<=4;++i)
for(int j=1;j<=4;++j) if(a[i][j]=='*')
{
rec[cnt][0]=i, rec[cnt][1]=j; cnt++;
}
dfs(0);
printf("Case #%d:\n", tt++);
for(int i=1;i<=4;++i)
{
for(int j=1;j<=4;++j) printf("%c", a[i][j]);
puts("");
}
}
return 0;
}