POJ 2676 数独

Sudoku

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions:24236   Accepted:11329   Special Judge

Description

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task. 

Input

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

Sample Input

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

Source

Southeastern Europe 2005

搜索算法训练——数独

初始化有:

hang[x][y]表示第x行是否能够填放y数字,

lie[x][y]表示第x列是否能够填放y数字,

square[x][y]表示第x个3*3格子能否填放y数字,

然后就是普通的dfs了

以下为代码:

#include <iostream>
#include<cstring>
using namespace std;
int a[15][15],ans[15][15];
bool square[10][10],lie[10][10],hang[10][10],flag[15][15];
bool check;
int ge(int x,int y)
{
    return(((x-1)/3)*3+(y-1)/3+1);
}

int print(){
    for(int i=1;i<=9;i++)
    {
        for(int j=1;j<=9;j++)
            cout<<ans[i][j];
        cout<<endl;
    }

}
int dfs(int x,int y)
{
    if(!check) return 0;
    if(x==0&&y==9)
    {
        print();
        check=false;
        return 0;
    }
    if(a[x][y]==0)
    for(int i=1;i<=9;i++)
    {
        if(square[ge(x,y)][i]&&lie[y][i]&&hang[x][i])
        {
            square[ge(x,y)][i]=false;
            lie[y][i]=false;
            hang[x][i]=false;
            ans[x][y]=i;

            if(y==1) dfs(x-1,9);else dfs(x,y-1);

            square[ge(x,y)][i]=true;
            lie[y][i]=true;
            hang[x][i]=true;
        }
    }
    if(a[x][y]!=0) if(y==1) dfs(x-1,9);else dfs(x,y-1);
}

int main()
{
//    freopen("in.txt","r",stdin);
    int t;
    cin>>t;
    while(t--)
    {
        memset(a,0,sizeof(a));
        memset(lie,true,sizeof(lie));
        memset(hang,true,sizeof(hang));
        memset(square,true,sizeof(square));
        for(int i=1;i<=9;i++)
        {
            char c[100];
            cin>>c;
            for(int j=1;j<=9;j++)
            {
                int x=c[j-1]-'0';
                a[i][j]=x;
                lie[j][x]=false;
                hang[i][x]=false;
                square[ge(i,j)][x]=false;
            }
        }
        check=true;
        memcpy(ans,a,sizeof(a));
        //print();
        dfs(9,9);
        /*for(int i=1;i<=9;i++)
            {

                for(int j=1;j<=9;j++)
                    cout<<ge(i,j);
                cout<<endl;
            }*/
        }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Ziwen_/article/details/82928095