数独这道题需要用到深搜,要注意数独的三个条件:1.每一行都是0~9不重复的数字 2.每一列都是0~9不重复的数字 3.每一个九宫格都是0~9不重复的数字 用vector装入数独中为零元素的坐标,在深搜时只经过这些点,当经过的点的个数等于vector的大小时,即可输出,然后返回
Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.
The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.
For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.
1 103000509 002109400 000704000 300502006 060000050 700803004 000401000 009205800 804000107
143628579 572139468 986754231 391542786 468917352 725863914 237481695 619275843 854396127
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
void dfs(int x,int y,int cur);
void show();
struct list
{
int x;
int y;
};
vector<struct list> v;
int judge(int x,int y);
bool check(int x,int y);
bool row[15][15],col[15][15],book[15][15],an[15][15];
int fig[15][15],dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int flag;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
flag=0;
v.clear();
memset(fig,0,sizeof(fig));
memset(row,false,sizeof(row));
memset(col,false,sizeof(col));
memset(book,false,sizeof(book));
memset(an,false,sizeof(an));
for(int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
{
scanf("%1d",&fig[i][j]);
if(fig[i][j]!=0)
{
an[judge(i,j)][fig[i][j]]=true;
row[i][fig[i][j]]=true;
col[j][fig[i][j]]=true;
}
else
{
struct list n;
n.x=i;
n.y=j;
v.push_back(n);
}
}
}
dfs(v[0].x,v[0].y,0);
}
return 0;
}
void dfs(int x,int y,int cur)
{
if(flag==1)
return;
if(cur==v.size())
{
show();
flag=1;
return ;
}
for(int j=1;j<10;j++)
{
if(row[x][j]==false&&col[y][j]==false&&an[judge(x,y)][j]==false)
{
fig[x][y]=j;
row[x][j]=true;
col[y][j]=true;
an[judge(x,y)][j]=true;
dfs(v[cur+1].x,v[cur+1].y,cur+1);
fig[x][y]=0;
row[x][j]=false;
col[y][j]=false;
an[judge(x,y)][j]=false;
}
}
}
void show()
{
for(int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
printf("%d",fig[i][j]);
putchar('\n');
}
}
bool check(int x,int y)
{
if(x>=1&&x<=9&&y>=1&&y<=9)
if(book[x][y]==false)
return true;
return false;
}
int judge(int x,int y)
{
if(x>=1&&x<=3&&y>=1&&y<=3)
return 1;
else if(x>=1&&x<=3&&y>=4&&y<=6)
return 2;
else if(x>=1&&x<=3&&y>=7&&y<=9)
return 3;
else if(x>=4&&x<=6&&y>=1&&y<=3)
return 4;
else if(x>=4&&x<=6&&y>=4&&y<=6)
return 5;
else if(x>=4&&x<=6&&y>=7&&y<=9)
return 6;
else if(x>=7&&x<=9&&y>=1&&y<=3)
return 7;
else if(x>=7&&x<=9&&y>=4&&y<=6)
return 8;
else if(x>=7&&x<=9&&y>=7&&y<=9)
return 9;
}