HOJ 2306 Tudoku --數獨

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

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

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");
        }
}


//只是能實現,算法不是最好,大家多提意見


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章