poj2676 Sudoku

Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14100   Accepted: 6961   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
题意:数独:玩家须要依据9×9盘面上的已知数字,推理出全部剩余空格的数字,并满足每一行、每一列、每个粗线宫内的数字均含1-9,不反复
思路: 爆搜,枚举,设置一个二维数组x[i][k]表示第i行有木有出现过数字k,y[j][k]表示第j列有木有出现过数字k,xg[(i/3)*3+j/3][k]表示第(i/3)*3+j/3个宫格有木有出现过 数字k。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define mem(a) memset(a,0,sizeof(a))
#define MAXN 100000
using namespace std;
char vw[9][9];
int x[9][9],y[9][9],xg[9][9];
bool vis;
int flag=0;
void dfs(int a,int b)
{
    int u=a*9+b+1;//这个公式相应以下的<span style="font-family: arial, 宋体, sans-serif;"> dfs(u/9,u%9),非常静止,自己想想为啥</span>
    if(a==9)
    {
        vis=true;//填好就标记直接返回
        for(int i=0; i<9; i++)
        {
            for(int j=0; j<9; j++)
                printf("%d",vw[i][j]+1);
            printf("\n");
        }
    }
    if(vis)<span style="font-family: arial, 宋体, sans-serif;">//填好就直接返回</span>
        return ;
    if(vw[a][b]!=-1)
    {
        dfs(u/9,u%9);
        return ;
    }
    for(int i=0; i<9&&!vis; i++)
    {
        if(!x[a][i]&&!y[b][i]&&!xg[(a/3)*3+b/3][i])
        {
            x[a][i]=y[b][i]=xg[(a/3)*3+b/3][i]=1;
            vw[a][b]=i;
            dfs(u/9,u%9);
            x[a][i]=y[b][i]=xg[(a/3)*3+b/3][i]=0;
            vw[a][b]=-1;
        }
    }
}
int main()
{
    int i,j,k,t;
    char ch;
    cin>>t;
    while(t--)
    {
        mem(x);
        mem(y);
        mem(xg);
        mem(vw);
        for(i=0; i<9; i++)
            for(j=0; j<9; j++)
            {
                cin>>ch;
                k=vw[i][j]=ch-'1';
                //printf("%d %d",k,vw[i][j]);
                if(ch-'0')
                {
                    x[i][k]=y[j][k]=xg[(i/3)*3+j/3][k]=1;
                }
            }
        vis=false;//标记填没填好
        dfs(0,0);
    }
    return 0;
}


posted on 2017-07-03 10:24  slgkaifa  阅读(145)  评论(0编辑  收藏  举报

导航