HOJ 2306 Tudoku --数独

本文介绍了一种解决简化版数独(Tudoku)的游戏算法。Tudoku是一种接近完成状态的数独游戏,大部分格子已被填好。文章详细解释了如何通过重复检查行列及九宫格内剩余数字来填充空缺。此外,提供了完整的C语言实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值