http://acm.hdu.edu.cn/showproblem.php?pid=5547
#include<stdio.h>
#include<iostream>
using namespace std;
char map[5][5];
bool book[5][5];
bool check(int x,int y,char c){
for(int j=1;j<=4;j++){ //行和列的判断
if(map[j][y]==c||map[x][j]==c)return false;
}
if(x%2==0&&y%2==0){ //2*2方格的右下角
if(map[x-1][y]==c||map[x-1][y-1]==c||map[x][y-1]==c)return false;
}
else if(x%2==0){ //左下角
if(map[x-1][y]==c||map[x][y+1]==c||map[x-1][y+1]==c)return false;
}
else if(y%2==0){//右上角
if(map[x][y-1]==c||map[x+1][y]==c||map[x+1][y-1]==c)return false;
}
else{//左上角
if(map[x+1][y]==c||map[x+1][y+1]==c||map[x][y+1]==c)return false;
}
return true;
}
void dfs(int x,int y)
{
if(x>4) //当把所有行搜完时输出
{
for(int i=1; i<=4; i++)
{
for(int j=1; j<=4; j++)
{
printf("%c",map[i][j]);
}
printf("\n");
}
return ;
}
if(map[x][y]!='*'){
if(y>=4)dfs(x+1,1);//一行行的搜,
else dfs(x,y+1);
}
else{
for(int i='1';i<='4';i++){
if(check(x,y,i)){
map[x][y]=i;
if(y>=4)dfs(x+1,1);
else dfs(x,y+1);
map[x][y]='*'; //不成立则回退
}
}
}
}
int main()
{
int _,ind=1;
scanf("%d",&_);
while(_--){
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
book[i][j]=false;
cin>>map[i][j];
}
}
printf("Case #%d:\n",ind++);
dfs(1,1);
}
return 0;
}