首页 > 代码库 > 数独 Sudoku poj 2676

数独 Sudoku poj 2676

Language:
Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 13909 Accepted: 6868 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
数独问题,直接暴搜,不知道问什么正着搜超时,倒着搜就过了。。。。
代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

bool ok;  //标记是否已经找到一个解
int mp[10][10];

void dfs(int x,int y)
{
    if (y==-1)  //倒着搜
    {
        x--;
        y=8;
    }
    if (x==-1&&y==8)
    {
        for (int i=0;i<9;i++)
        {
            for (int j=0;j<9;j++)
                printf("%d",mp[i][j]);
            printf("\n");
        }
        ok=true;   //找到一组解就标记
        return ;
    }
    if (!ok)
    {
        if (!mp[x][y])
        {
            for (int t=1;t<=9;t++)
            {
                int flag=1;
                for (int i=8;i>=0;i--)
                {
                    if (mp[x][i]==t)
                    {
                        flag=0;
                        break;
                    }
                    if (mp[i][y]==t)
                    {
                        flag=0;
                        break;
                    }
                }
                int xx=3*(x/3);
                int yy=3*(y/3);
                for (int i=0;i<3;i++)
                {
                    for (int j=0;j<3;j++)
                    {
                        if (mp[xx+i][yy+j]==t)
                        {
                            flag=0;
                            break;
                        }
                    }
                }
                if (flag)
                {
                    mp[x][y]=t;
                    dfs(x,y-1);
                    mp[x][y]=0;
                }
            }
        }
        else
            dfs(x,y-1);
    }
    return ;
}

int main()
{
    int i,j,cas;
    scanf("%d",&cas);
    while (cas--)
    {
        for (i=0;i<9;i++)
            for (j=0;j<9;j++)
                scanf("%1d",&mp[i][j]);
        ok=false;   
        dfs(8,8);
    }
    return 0;
}
/*
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
*/



数独 Sudoku poj 2676