題目:
Tom is a master in several mathematical-theoretical disciplines. He recently founded a research-lab at our university and teaches newcomers like Jim. In the first lesson he explained the game of Tudoku to Jim. Tudoku is a straight-forward variant of Sudoku, because it consists of a board where almost all the numbers are already in place. Such a board is left over when Tom stops solving an ordinary Sudoku because of being too lazy to fill out the last few straight-forward cells. Now, you should help Jim solve all Tudokus Tom left for him.
Sudoku is played on a 9 × 9 board that is divided into nine different 3 × 3 blocks. Initially, it contains only a few numbers and the goal is to fill the remaining cells so that each row, column, and 3 × 3 block contains every number from 1 to 9. This can be quite hard but remember that Tom already filled most cells. A resulting Tudoku board can be solved using the following rule repeatedly: if some row, column or 3 × 3 block contains exactly eight numbers, fill in the remaining one.
In the following example, three cells are still missing. The upper left one cannot be determined directly because neither in its row, column, or block, there are eight numbers present. The missing number for the right cell can be determined using the above rule, however, because its column contains exactly eight numbers. Similarly, the number for the lower-most free cell can be determined by examining its row. Finally, the last free cell can be filled by either looking at its row, column or block.
7 | 5 | 3 | 2 | 8 | 4 | 6 | 9 | 1 |
4 | 8 | 2 | 9 | 1 | 6 | 5 | 3 | 7 |
1 | 9 | 6 | 7 | 5 | 3 | 8 | 4 | 2 |
9 | 3 | 1 | 6 | 4 | 2 | 5 | ||
2 | 7 | 5 | 4 | 9 | 1 | 3 | 8 | 6 |
6 | 4 | 8 | 3 | 2 | 1 | 7 | 9 | |
5 | 6 | 7 | 3 | 4 | 9 | 2 | 1 | 8 |
8 | 2 | 4 | 1 | 7 | 5 | 9 | 6 | 3 |
3 | 1 | 9 | 6 | 2 | 8 | 7 | 5 | 4 |
Input
The first line contains the number of scenarios. For each scenario the input contains nine lines of nine digits each. Zeros indicate the cells that have not been filled by Tom and need to be filled by you. Each scenario is terminated by an empty line.
Output
The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then, print the solved Tudoku board in the same format that was used for the input, except that zeroes are replaced with the correct digits. Terminate the output for the scenario with a blank line.
Sample Input
2 000000000 817965430 652743190 175439820 308102950 294856370 581697240 903504610 746321580 781654392 962837154 543219786 439182675 158976423 627543918 316728549 895461237 274395861
Sample Output
Scenario #1: 439218765 817965432 652743198 175439826 368172954 294856371 581697243 923584617 746321589 Scenario #2: 781654392 962837154 543219786 439182675 158976423 627543918 316728549 895461237 274395861
題意:
給你一個數字N,代表有N個數獨表格,其中0代表是你要填寫的數字,讓你填寫出來正確的數獨;
思路:
直接深搜,注意剪枝,否則容易超時;
代碼如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,flag;
char map[10][10];
int a[10][10];//存儲數獨表格;
int h[10][10];//h[x][y]==1:表示第x行中的數字y已經被使用過了;
int l[10][10];//l[x][y]==1:表示第x列中的數字y已經被使用過了;
void init()//初始化;
{
memset(map,0,sizeof map);
memset(a,0,sizeof a);
memset(h,0,sizeof h);
memset(l,0,sizeof l);
}
//判斷同一個小的3*3的格子裏的數是否滿足數獨的規則;
int check(int xx,int yy,int k)
{
int x=xx/3;
int y=yy/3;
x=x*3;
y=y*3;
for(int i=x; i<x+3; i++)
{
for(int j=y; j<y+3; j++)
{
if(a[i][j]==k)
return 0;
}
}
return 1;
}
void print()//輸出結果;
{
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
printf("\n");//注意輸出的格式;
return ;
}
void dfs(int x)
{
if(flag==1)//剪枝;
return ;
if(x==81)//已經填滿了;
{
flag=1;
print();
return ;
}
int xx=x/9;//行;
int yy=x%9;//列;
if(a[xx][yy]==0)//需要你填寫數字;
{
for(int i=1; i<=9; i++)//遍歷9個數字,嘗試要填寫的數字;
{
if(h[xx][i]==0&&l[yy][i]==0&&check(xx,yy,i))
{//行和列中數字i都沒有被使用過,且在小格子中也沒有用過;
a[xx][yy]=i;//數字i填入數獨表格中;
h[xx][i]=1;//標記爲使用過;
l[yy][i]=1;
dfs(x+1);//搜索下一個位置;
if(flag==1)//剪枝;
return;
a[xx][yy]=0;//還原;
h[xx][i]=0;
l[yy][i]=0;
}
}
}
else
dfs(x+1);//不需要填寫,直接搜索下一個位置;
if(flag==1)//剪枝;
return ;
return ;
}
int main()
{
scanf("%d",&n);
int tt=1;
while(n--)
{
init();
for(int i=0; i<9; i++)
{
scanf("%s",map[i]);
for(int j=0; j<9; j++)
{
a[i][j]=map[i][j]-'0';
h[i][a[i][j]]=1;//標記已經使用過的數字;
l[j][a[i][j]]=1;
}
}
flag=0;
printf("Scenario #%d:\n",tt++);
dfs(0);
}
return 0;
}