Tudoku

FJNU.1937

Description
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.


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

Source
TUD Programming Contest 2006, Darmstadt, Germany

My Program

#include < iostream >
#define  N 9
using   namespace  std;
int  map[N][N];

int  main()
{
    
int i,j,k,n,f,p,q,t,r;
    
int temp[10]={0},now;
    
char c;
    cin
>>n;
    
for(k=0;k<n;k++)
    
{
        f
=0;
        
for(i=0;i<N;i++)
            
for(j=0;j<N;j++)
            
{
                cin
>>c;
                map[i][j]
=c-'0';
                
if(map[i][j]==0)
                    f
++;
            }

        
while(f!=0)
        
{
            
for(i=0;i<N;i++)
            
{
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(j=0;j<N;j++)
                    
if(map[i][j]==0)
                    
{
                        now
++;
                        t
=j;
                        
if(now>1)
                            
break;
                    }

                    
else
                        temp[(map[i][j]
-1)]=1;
                
if(now==1)        //在某行上如果只有一个为0则填满。
                {
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[i][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
for(i=0;i<N;i++)
            
{
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(j=0;j<N;j++)
                    
if(map[j][i]==0)
                    
{
                        now
++;
                        t
=j;
                        
if(now>1)
                            
break;
                    }

                    
else
                        temp[(map[j][i]
-1)]=1;
                
if(now==1)        //在某列上如果只有一个为0则填满。
                {                            
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[t][i]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
//    开始布阵
            
// i = 0 to 2;j= 0 to 2
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=0;i<3;i++)
                    
for(j=0;j<3;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 0 to 2;j= 3 to 6
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=0;i<3;i++)
                    
for(j=3;j<6;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 0 to 2;j= 6 to 9
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=0;i<3;i++)
                    
for(j=6;j<9;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 3 to 6;j= 0 to 2
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=3;i<6;i++)
                    
for(j=0;j<3;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 3 to 6;j= 3 to 6
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=3;i<6;i++)
                    
for(j=3;j<6;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 3 to 6;j= 6 to 9
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=3;i<6;i++)
                    
for(j=6;j<9;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 6 to 9;j= 0 to 2
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=6;i<9;i++)
                    
for(j=0;j<3;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 6 to 9;j= 3 to 6
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=6;i<9;i++)
                    
for(j=3;j<6;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 6 to 9;j= 6 to 9
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=6;i<9;i++)
                    
for(j=6;j<9;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

        }

        cout
<<"Scenario #"<<k+1<<":"<<endl;
        
for(i=0;i<N;i++)
        
{
            
for(j=0;j<N;j++)
                cout
<<map[i][j];
            cout
<<endl;
        }

        cout
<<endl;
    }

    
return 0;
}

YOYO's Note:
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄它是华丽的分隔线

【题意简述】

数独游戏,是在9*9的棋盘中填满数字,每行、每列以及每个小9格里面的数字不能相同。给定几个数独题目,编写一个程序填满。


【粗略分析】
有点累的模拟题。先读入棋盘数字,之后由以下部分组成:
1.每行、每列、每个小九格遍历一次,看是否有只剩一个空可以填,如果有则填满,继续2。
2.看是否已经填完所有的空,是则退出,不是则返回1。
因为数字是一连串输入的,所以我们用读字符的方式一个个读入并转换成数字存入二维数组map中,同时记录为0(即表示需要填的空)的个数f。接着开始永真循环,判定这个循环结束的条件就是f==0。(f在每次填上新空的时候就自减)。
程序比较简单,但是懒得想小九格怎么写,于是写了9个分别判断9个小格的循环 = = +
懒虽懒,但是好歹也AC了嘛……


【C++源代码】

#include < iostream >
#define  N 9
using   namespace  std;
int  map[N][N];

int  main()
{
    
int i,j,k,n,f,p,q,t,r;
    
int temp[10]={0},now;
    
char c;
    cin
>>n;
    
for(k=0;k<n;k++)
    
{
        f
=0;
        
for(i=0;i<N;i++)
            
for(j=0;j<N;j++)
            
{
                cin
>>c;
                map[i][j]
=c-'0';
                
if(map[i][j]==0)
                    f
++;
            }

        
while(f!=0)
        
{
            
for(i=0;i<N;i++)
            
{
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(j=0;j<N;j++)
                    
if(map[i][j]==0)
                    
{
                        now
++;
                        t
=j;
                        
if(now>1)
                            
break;
                    }

                    
else
                        temp[(map[i][j]
-1)]=1;
                
if(now==1)        //在某行上如果只有一个为0则填满。
                {
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[i][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
for(i=0;i<N;i++)
            
{
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(j=0;j<N;j++)
                    
if(map[j][i]==0)
                    
{
                        now
++;
                        t
=j;
                        
if(now>1)
                            
break;
                    }

                    
else
                        temp[(map[j][i]
-1)]=1;
                
if(now==1)        //在某列上如果只有一个为0则填满。
                {                            
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[t][i]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
//    开始布阵
            
// i = 0 to 2;j= 0 to 2
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=0;i<3;i++)
                    
for(j=0;j<3;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 0 to 2;j= 3 to 6
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=0;i<3;i++)
                    
for(j=3;j<6;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 0 to 2;j= 6 to 9
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=0;i<3;i++)
                    
for(j=6;j<9;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 3 to 6;j= 0 to 2
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=3;i<6;i++)
                    
for(j=0;j<3;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 3 to 6;j= 3 to 6
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=3;i<6;i++)
                    
for(j=3;j<6;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 3 to 6;j= 6 to 9
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=3;i<6;i++)
                    
for(j=6;j<9;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 6 to 9;j= 0 to 2
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=6;i<9;i++)
                    
for(j=0;j<3;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 6 to 9;j= 3 to 6
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=6;i<9;i++)
                    
for(j=3;j<6;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

            
if(f==0)
                
break;
            
// i = 6 to 9;j= 6 to 9
            {
                memset(temp,
0,sizeof(temp));
                now
=0;
                
for(i=6;i<9;i++)
                    
for(j=6;j<9;j++)
                        
if(map[i][j]==0)
                        
{
                            now
++;
                            r
=i;
                            t
=j;
                            
if(now>1)
                                
break;
                        }

                        
else
                            temp[(map[i][j])
-1]=1;
                
if(now==1)
                
{
                    
for(j=0;j<N;j++)
                        
if(temp[j]==0)
                        
{
                            map[r][t]
=j+1;
                            
break;
                        }

                    f
--;
                }

            }

        }

        cout
<<"Scenario #"<<k+1<<":"<<endl;
        
for(i=0;i<N;i++)
        
{
            
for(j=0;j<N;j++)
                cout
<<map[i][j];
            cout
<<endl;
        }

        cout
<<endl;
    }

    
return 0;
}

【注意事项】

※ 输出格式。记得输出一个MAP之后要换行。


【点评】

 模拟题。没什么好说的……这种题型通常不会给特殊数据,也不会出现有多解的那种地图。只要过了sample一般都可以AC了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值