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
相同题目求法链接,可以参考,思路完全相同:https://blog.csdn.net/queque_heiya/article/details/104109187
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
#define inf 1<<25
#define LL long long
using namespace std;
//考虑为bool类型也可以实现
int row[10][10];//判断row[i][x],在i行x是否出现(bool)
int col[10][10];//判断 col[j][y],在j列y是否出现(bool)
int map[10][10];//处理输入
int small[10][10];//处理3*3格子(bool)
int f(int x,int y){
return 3*((x-1)/3)+(y-1)/3+1;//返回x,y出现在哪个具体的网格
}
void init(){
int i,j;
char ch;
memset(row,0,sizeof(row));
memset(col,0,sizeof(col));
memset(small,0,sizeof(small));
for(i=1;i<=9;i++){
for(j=1;j<=9;j++){
scanf("%c",&ch);
map[i][j]=ch-'0';
if(map[i][j]){//不处理0的情况
int k;
k=f(i,j);
row[i][map[i][j]]=1;//在i行出现map[i][j];
col[j][map[i][j]]=1;//在j列出现map[i][j];
small[k][map[i][j]]=1;//map[i][j]属于第k个网格
}
}
getchar();
}
}
int dfs(int x,int y){
if(x==10)
return 1;
int flag=0;
if(map[x][y]){
if(y==9)
flag=dfs(x+1,1);
else
flag=dfs(x,y+1);
if(flag)
return 1;
else
return 0;
}
else{
int k=f(x,y);
for(int i=1;i<=9;i++)
if(!row[x][i]&&!col[y][i]&&!small[k][i]){
map[x][y]=i;
row[x][i]=1;
col[y][i]=1;
small[k][i]=1;
if(y==9)
flag=dfs(x+1,1);
else
flag=dfs(x,y+1);
if(!flag){
map[x][y]=0;
row[x][i]=0;
col[y][i]=0;
small[k][i]=0;
}
else
return 1;
}
}
return 0;
}
int main(){
int t,k=1;
scanf("%d",&t);
while(t--){
getchar();
init();//处理输入问题
dfs(1,1);//从当前的位置开始搜索dfs();
printf("Scenario #%d:\n",k++);
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++)
printf("%d",map[i][j]);
printf("\n");
}
printf("\n");//注意按照格式输出
}
}