#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
char e;
int a[10][10];
struct node{
int x,y;
}f[100]; //建个结构体用来储存?的坐标,深搜时只要一个个来就可以了
int t,flag;
int judge(int k,int s) //判断是否合理,就是查行列及九个格子是否重复
{
int xx,yy,i,j;
for(i=0;i<9;i++)
if(a[f[s].x][i]==k)
return 0;
for(i=0;i<9;i++)
if(a[i][f[s].y]==k)
return 0;
xx=f[s].x/3*3;
yy=f[s].y/3*3;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(a[xx+i][yy+j]==k)
return 0;
return 1;
}
void dfs(int s)
{
int i,j;
if(s==t)
{
for(i=0;i<9;i++)
{
printf("%d",a[i][0]);
for(j=1;j<9;j++)
printf(" %d",a[i][j]);
printf("\n");
}
flag=1;
return;
}
for(i=1;i<=9;i++)
{
if(judge(i,s)==0)
continue;
a[f[s].x][f[s].y]=i;
dfs(s+1);
if(flag==1)
return;
a[f[s].x][f[s].y]=0;
}
}
int main()
{
int tt=0;
while(cin>>e) //用scanf输入有点烦人就改成cin了,一开始用字符串的,忘了空格。。
{
int i,j;
t=0;
if(e=='?')
{
f[t].x=f[t].y=0;
t++;
a[0][0]=0;
}
else
a[0][0]=e-'0';
for(i=0;i<9;i++)
for(j=0;j<9;j++)
if(i==0&&j==0)
continue;
else
{
cin>>e;
if(e=='?')
{
f[t].x=i;
f[t].y=j;
t++;
a[i][j]=0;
}
else
a[i][j]=e-'0';
}
flag=0;
if(tt!=0) //格式啊,一开始没注意就PE了
printf("\n");
tt++;
dfs(0);
}
return 0;
}
hdu 1426 Sudoku Killer
最新推荐文章于 2020-03-03 18:33:21 发布