HOJ 2306 Tudoku --数独

来源:互联网 发布:c语言中exit 0 编辑:程序博客网 时间:2024/05/22 11:45
Time limit : 1 sec Memory limit : 64 M


Tom is a master in several mathematical-theoretical disciplines. He recently founded a research-lab at ouruniversity 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 numbersare already in place. Such a board is left over when Tom stops solving an ordinary Sudoku because of beingtoo lazy to fill out the last few straight-forward cells. Now, you should help Jim solve all Tudokus Tom leftfor him.

Problem

Sudoku is played on a 9 * 9 board that is divided into nine different 3 * 3 blocks. Initially, it containsonly a few numbers and the goal is to fill the remaining cells so that each row, column, and 3 * 3 blockcontains 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 * 3block 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 directlybecause neither in its row, column, or block, there are eight numbers present. The missing number forthe right cell can be determined using the above rule, however, because its column contains exactly eightnumbers. 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.

Input

The first line contains the number of scenarios. For each scenario the input contains nine lines of nine digitseach. Zeros indicate the cells that have not been filled by Tom and need to be filled by you. Each scenariois terminated by an empty line.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number ofthe scenario starting at 1. Then, print the solved Tudoku board in the same format that was used for theinput, except that zeroes are replaced with the correct digits. Terminate the output for the scenario with ablank line.

Sample Input

2000000000817965430652743190175439820308102950294856370581697240903504610746321580781654392962837154543219786439182675158976423627543918316728549895461237274395861

Sample Output

Scenario #1:439218765817965432652743198175439826368172954294856371581697243923584617746321589Scenario #2:781654392962837154543219786439182675158976423627543918316728549895461237274395861

Solution:

#include <stdio.h>//分别计算行、列、块中0的个数(即需要填写位置的个数),并返回总的0的个数int count0(int a[][9],int count[][9])  {        int m = 0,count1 = 0;        for(int i=0;i<3;i++)        {                for(int j=0;j<9;j++)                {                        count[i][j] = 0;                }        }        for(int i=0;i<9;i++)        {                        for(int j=0;j<9;j++)                    {                                m = i/3*3+j/3;                                  if(a[i][j]  == 0)                                       {                                                count[0][i]++;                                                count[1][j]++;                                                count[2][m]++;                                                count1++;                                }                        }        }               return count1;}void clear(int a[]){        for(int i=0;i<9;i++)        {                a[i] = 0;        }}void todoku(int a[][9]){        int count[3][9] = {0};        int pos1,pos2,m,n,flag=0;        int number[9] = {0};        while(count0(a,count))        {                for(int i=0;i<9;i++)                {                        //处理0个数为1的行                        if(count[0][i] == 1)                        {                                for(int j=0;j<9;j++)                                {                                        if(a[i][j] == 0)                                        {                                                pos1 = j;                                                flag=1;                                        }                                        else                                                number[a[i][j]-1] = 1;                                }                                if(flag)                                {                                        for(int j=0;j<9;j++)                                        {                                                if(number[j] == 0)                                                        a[i][pos1] = j+1;                                        }                                                                                clear(number);                                        flag = 0;                                }                        }//end if                                        //处理0个数为1的列                        if(count[1][i] == 1)                        {                                for(int j=0;j<9;j++)                                {                                        if(a[j][i] == 0)                                         {                                                pos1 = j;                                                flag = 1;                                        }                                        else                                        {                                                number[a[j][i]-1] = 1;                                        }                                }                                if(flag)                                {                                        for(int j=0;j<9;j++)                                        {                                                if(number[j] == 0)                                                        a[pos1][i] = j+1;                                        }                                                                        clear(number);                                        flag = 0;                                }                        }//end if                        //处理0个数为1的块                        if(count[2][i] == 1)                        {                                m=i/3*3;                                n=i%3*3;                                                                for(int j=0;j<3;j++)                                {                                        for(int k=0;k<3;k++)                                        {                                                if(a[m+j][n+k] == 0)                                                {                                                        pos1=m+j;                                                        pos2=n+k;                                                        flag = 1;                                                }                                                else                                                        number[a[m+j][n+k]-1] = 1;                                        }                                       }                                if(flag)                                {                                        for(int j=0;j<9;j++)                                        {                                                if(number[j] == 0)                                                {                                                               a[pos1][pos2] = j+1;                                                }                                               }                                                                        clear(number);                                        flag = 0;                                }                        }//end if                }//end for        }//end while       }int main(){        int num,a[9][9]={0};        scanf("%d",&num);        for(int i=1;i<=num;i++)        {                for(int m=0;m<9;m++)                {                        for(int n=0;n<9;n++)                        {                                scanf("%1d",&a[m][n]);                        }                }                       todoku(a);                printf("Scenario #%d:\n",i);                for(int m=0;m<9;m++)                {                        for(int n=0;n<9;n++)                        {                                printf("%d",a[m][n]);                        }                        printf("\n");                }                printf("\n");        }}


//只是能实现,算法不是最好,大家多提意见


原创粉丝点击