

7 1 2 ? 6 ? 3 5 8 ? 6 5 2 ? 7 1 ? 4 ? ? 8 5 1 3 6 7 2 9 2 4 ? 5 6 ? 3 7 5 ? 6 ? ? ? 2 4 1 1 ? 3 7 2 ? 9 ? 5 ? ? 1 9 7 5 4 8 6 6 ? 7 8 3 ? 5 1 9 8 5 9 ? 4 ? ? 2 3
7 1 2 4 6 9 3 5 8 3 6 5 2 8 7 1 9 4 4 9 8 5 1 3 6 7 2 9 2 4 1 5 6 8 3 7 5 7 6 3 9 8 2 4 1 1 8 3 7 2 4 9 6 5 2 3 1 9 7 5 4 8 6 6 4 7 8 3 2 5 1 9 8 5 9 6 4 1 7 2 3
#include <iostream>
using namespace std;
struct Point
{
int x;
int y;
} point[100];
int map[10][10];
int num;
bool flag;
bool check(int k,int cur)
{
int i,j,x,y;
for(i=0; i<9; i++)//判断所在列和行
{
if(map[point[cur].x][i]==k||map[i][point[cur].y]==k)
{
return false;
}
}
x=(point[cur].x/3)*3;
y=(point[cur].y/3)*3;
for(i=x; i<x+3; i++)
{
for(j=y; j<y+3; j++)
{
if(map[i][j]==k)
{
return false;
}
}
}
return true;
}
void DFS(int step)
{
if(step==num)
{
for(int i=0; i<9; i++)
{
for(int j=0; j<8; j++)
{
cout<<map[i][j]<<" ";
}
cout<<map[i][8]<<endl;
}
flag=true;
return ;
}
if(flag)
return ;
for(int i=1; i<=9; i++)
{
if(check(i,step))
{
map[point[step].x][point[step].y]=i;
DFS(step+1);
map[point[step].x][point[step].y]=0;
}
}
return;
}
int main()
{
int cas=0;
char s;
while(cin>>s)
{
num=0;
if(s=='?')
{
point[num].x=0;
point[num].y=0;
num++;
map[0][0]=0;
}
else
{
map[0][0]=s-'0';
}
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
if(i==0&&j==0)
continue;
cin>>s;
if(s=='?')
{
point[num].x=i;
point[num].y=j;
num++;
map[i][j]=0;
}
else
{
map[i][j]=s-'0';
}
}
}
flag=false;
if(cas++)
cout<<endl;
DFS(0);
}
return 0;
}
#include <stdio.h>
struct Point
{
int x;
int y;
} point[100];
int map[10][10];
int flag,num;
int check(int k,int cur)
{
int i,j,x,y;
for(i=0;i<9;i++)
{
if(map[point[cur].x][i]==k||map[i][point[cur].y]==k)
{
return 0;
}
}
x=(point[cur].x/3)*3;
y=(point[cur].y/3)*3;
for(i=x;i<x+3;i++)
{
for(j=y;j<y+3;j++)
{
if(map[i][j]==k)
{
return 0;
}
}
}
return 1;
}
void DFS(int step)
{
int i,j;
if(step==num)
{
for(i=0;i<9;i++)
{
for(j=0;j<8;j++)
{
printf("%d ",map[i][j]);
}
printf("%d\n",map[i][8]);
}
flag=1;
return ;
}
else
{
for(i=1;i<=9;i++)
{
if(check(i,step)&&!flag)
{
map[point[step].x][point[step].y]=i;
DFS(step+1);
map[point[step].x][point[step].y]=0;
}
}
}
return;
}
int main()
{
int i,j,cas=0;
char s[3];
while(scanf("%s",s)!=EOF)
{
num=0;
if(s[0]=='?')
{
point[num].x=0;
point[num].y=0;
num++;
map[0][0]=0;
}
else
{
map[0][0]=s[0]-'0';
}
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
if(i==0&&j==0)
{
continue;
}
scanf("%s",s);
if(s[0]=='?')
{
point[num].x=i;
point[num].y=j;
num++;
map[i][j]=0;
}
else
{
map[i][j]=s[0]-'0';
}
}
}
flag=0;
if(cas++)
{
printf("\n");
}
DFS(0);
}
return 0;
}
HDU 1426 Sudoku Killer(数独,划分区域是关键)
原文:http://blog.csdn.net/hurmishine/article/details/51333913