poj 2918Tudoku(暴力。。)

Tudoku
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1911   Accepted: 1060

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.

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


周赛的时候没有做出来,很早就在看这个题目,然后开始用dfs写,调了很久
很久都没用出来,今天突然发现了一个条件。The missing number for the
right cell can be determined using the above rule, however, because its 
column contains exactly eight numbers 剩下的我就不说了。。。
题目还是得先看清再做,事半功倍。

题目地址:Tudoku

AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int mp[10][10];
char str[10];
int sum;

int over()
{
    int i,j;
    for(i=1; i<=9; i++)
        for(j=1; j<=9; j++)
            if(!mp[i][j])
                return 0;
    return 1;
}

int ist1(int r,int c)    //行
{
    sum=0;
    int i;
    for(i=1; i<=9; i++)
    {
        if(i==c) continue;
        if(!mp[r][i]) return 0;
        sum+=mp[r][i];
    }
    return 1;
}

int ist2(int r,int c)    //列
{
    sum=0;
    int i;
    for(i=1; i<=9; i++)
    {
        if(i==r) continue;
        if(!mp[i][c]) return 0;
        sum+=mp[i][c];
    }
    return 1;
}

int ist3(int r,int c)   //小宫格
{
    sum=0;
    int i,j,a,b;
    a=(r-1)/3*3+1;
    b=(c-1)/3*3+1;
    for(i=a; i<=a+2; i++)
        for(j=b; j<=b+2; j++)
        {
            if(i==r&&j==c) continue;
            if(!mp[i][j]) return 0;
            sum+=mp[i][j];
        }
    return 1;
}

int main()
{
    int tes,cas,i,j;
    cin>>tes;

    for(cas=1; cas<=tes; cas++)
    {
        for(i=1; i<=9; i++)
        {
            cin>>str;
            for(j=1; j<=9; j++)
                mp[i][j]=str[j-1]-'0';
        }

        while(1)
        {
            if(over()) break;
            int flag=0;
            for(i=1; i<=9; i++)
            {
                for(j=1; j<=9; j++)
                {
                    if(!mp[i][j]&&(ist1(i,j)||ist2(i,j)||ist3(i,j)))
                    {
                        mp[i][j]=45-sum;
                        flag=1;
                        break;
                    }
                }
                if(flag) break;
            }
        }

        printf("Scenario #%d:\n",cas);
        for(i=1;i<=9;i++)
        {
            for(j=1;j<=9;j++)
                cout<<mp[i][j];
            cout<<endl;
        }
        if(cas<tes) cout<<endl;
    }
    return 0;
}



你可能感兴趣的:(个人赛)