Sudoku
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 62 Accepted Submission(s) : 21
Special Judge
Problem Description
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.
![](https://i-blog.csdnimg.cn/blog_migrate/56e7e6182093bbca488d6c73ba833af6.jpeg)
Input
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.
Output
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.
Sample Input
1 103000509 002109400 000704000 300502006 060000050 700803004 000401000 009205800 804000107
Sample Output
143628579 572139468 986754231 391542786 468917352 725863914 237481695 619275843 854396127
题意就是数独填数,挺好理解,做的话深度搜索,要考虑的东西挺多:行上没有、列上没有、小方块上没有。行列还好说,方块是要判断的,我用的是最笨的方法,单列一个函数判断,自上往下从左到右依次设为1-9。再一个,判断某点能不能放,是否为0就可以,但我没这样。我是用一个结构体存起来可以放得点的坐标,直接按路径搜就好。深搜函数上就是判断该点能否放1-9,能放就放,放完回溯。但是直接回溯的话会出毛病的,因为他会把放完的又归零了,白忙活,所以回溯前加一个判断,用flag也好,返回值也好,如果已经放完了就不继续了,return。
这里给两个代码,一个是标记行列块的,另一个是看到人家直接判断(x,y)能否放i。都是AC码。
一、
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
struct node
{
int x;
int y;
};
node road[100];
int p[10][10];
int m;
int h[10][10];
int l[10][10];
int q[10][10];
int sqare(int x,int y)
{
if(x>=1&&x<=3)
{
if(y>=1&&y<=3)
return 1;
if(y>=4&&y<=6)
return 2;
if(y>=7&&y<=9)
return 3;
}
else if(x>=4&&x<=6)
{
if(y>=1&&y<=3)
return 4;
if(y>=4&&y<=6)
return 5;
if(y>=7&&y<=9)
return 6;
}
else if(x>=7&&x<=9)
{
if(y>=1&&y<=3)
return 7;
if(y>=4&&y<=6)
return 8;
if(y>=7&&y<=9)
return 9;
}
return -1;
}
int ok(int x,int y,int s,int i)
{
if(q[s][i]==0 && h[x][i]==0 && l[y][i]==0)
return 1;
return 0;
}
int flag;
void ioan(int k)
{
if(k==0)
{
flag=1;
return ;
}
int x=road[k].x;
int y=road[k].y;
int s=sqare(x,y);
for(int i=1;i<=9;i++)
{
if(ok(x,y,s,i))// OK 了 才能放
{
p[x][y]=i;
q[s][i]=1;
h[x][i]=1;
l[y][i]=1;
ioan(k-1);
if(flag==1) //立 flag ,如果到头了,就不在回溯,以后的不管了
return ;
p[x][y]=0;//这一个回溯 本来一直在纠结加不加,如果加了最后结果都还原了 然后在上一行判断
q[s][i]=0;//以下的回溯是肯定要加的
h[x][i]=0;
l[y][i]=0;
}
}
}
int main()
{
//freopen("D:\\aaa.txt","r",stdin);
int n;
cin>>n;
while(n--)
{
char ch;
int s;
flag=0;
m=0;
memset(q,0,sizeof(q));
memset(h,0,sizeof(h));
memset(l,0,sizeof(l));
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
scanf(" %c",&ch);
p[i][j]=ch-'0';
s=sqare(i,j);// 块
q[s][p[i][j]]=1;//标记
h[i][p[i][j]]=1;
l[j][p[i][j]]=1;
if(p[i][j]==0)
{
m++; //路径
road[m].x=i;
road[m].y=j;
}
}
}
ioan(m);
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
printf("%d",p[i][j]);
}
printf("\n");
}
}
return 0;
}
二、
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
struct node
{
int x;
int y;
};
node road[200];
int p[10][10];
int m,flag;
int judge(int x,int y,int k)
{
for(int i=1;i<=9;i++)
{
if(p[x][i]==k)
return 0;
if(p[i][y]==k)
return 0;
}
int xx=((x-1)/3)*3;
int yy=((y-1)/3)*3;
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
if(p[i+xx][j+yy]==k)
return 0;
}
}
return 1;
}
int ioan(int k)
{
if(k==0)
{
flag=1;
return 1;
}
for(int i=1;i<=9;i++)
{
int x=road[k].x;
int y=road[k].y;
if(judge(x,y,i))
{
p[x][y]=i;
int f=ioan(k-1);
//cout<<".............. "<<k<<endl;
if(f)
return 1;
p[x][y]=0;
}
}
return 0;
}
int main()
{
//freopen("D:\\aaa.txt","r",stdin);
int n;
cin>>n;
while(n--)
{
char ch;
m=0;
flag=0;
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
//scanf(" %c",&ch);
cin>>ch;
p[i][j]=ch-'0';
if(p[i][j]==0)
{
m++; //路径
road[m].x=i;
road[m].y=j;
}
}
}
//cout<<"m= "<<m<<endl;
ioan(m);
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
printf("%d",p[i][j]);
}
printf("\n");
}
}
return 0;
}