Sudoku

Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12157 Accepted: 6070 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

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <vector>
#include <bitset>
#include <cstdio>
#include <string>
#include <numeric>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define eps 1e-8
#define inf 0x7fffffff
#define debug puts("BUG")
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define maxn 20

int  Map[maxn][maxn];//储存最初状态和结束状态
bool via[maxn][maxn];//表示哪个坐标是空位
bool judgeh[maxn][maxn],judgel[maxn][maxn],judgex[maxn][maxn][maxn];
//judgeh[i][j]是表示第i行值为j时是否存在,同理,judgel表示的是列,judgex代表的是,第几行第几列的3*3的数字方格中,某个数值是否存在
/**
  *     1 2 3 
  *     4 5 6
  *     7 8 9
  *     这就是数字方格的编号
  */
bool flag;//表示是否搜索出结果

void init()//初始化
{
    memset(via,0,sizeof(via));
    memset(judgeh,0,sizeof(judgeh));
    memset(judgel,0,sizeof(judgel));
    memset(judgex,0,sizeof(judgex));
    flag=0;
}

int f(int x)//将9*9的数字行列变换成3*3的数字方格行列
{
    if(x<=3) return 1;
    if(x<=6) return 2;
    return 3;
}

void dfs(int j,int k)//深搜,第j行第k列的位置
{
    if(j==10)//表示搜索完毕
    {
        flag=1;
        return;
    }
    if(via[j][k])//表示当前位置的值是初始时给定的值
    {//从左到右,从上往下搜索
        if(k==9)
            dfs(j+1,1);
        else
            dfs(j,k+1);
    }
    else//表示该位置是有待填充的位置
    {
        via[j][k]=1;//填充
        int a=f(j);//压缩行
        int b=f(k);//压缩列

        for(int x=1; x<=9&&!flag; x++)//列举九种值
        {
            if(!judgeh[j][x]&&!judgel[k][x]&&!judgex[a][b][x])//能填充的条件
            {
                Map[j][k]=x;//填充
                judgeh[j][x]=1;
                judgel[k][x]=1;
                judgex[a][b][x]=1;
                if(k==9)//搜索顺序
                    dfs(j+1,1);
                else
                    dfs(j,k+1);
                judgeh[j][x]=0;//恢复现场
                judgel[k][x]=0;
                judgex[a][b][x]=0;
            }
        }
        via[j][k]=0;//恢复现场
    }
}

void print()//输出函数
{
    for(int j=1; j<=9; j++)
    {
        for(int k=1; k<=9; k++)
            printf("%d",Map[j][k]);
        printf("\n");
    }
}

int main()
{
    int icas;
    scanf("%d",&icas);
    while(icas--)
    {
        init();//初始化,被坑过几次 = =!
        char str[maxn+10];
        for(int j=1; j<=9; j++)
        {
            scanf("%s",str+1);//一行一行读,防止出现一行之后有冗余
            for(int k=1; k<=9; k++)
            {
                int x=str[k]-'0';
                if(x>0)
                {
                    Map[j][k]=x;//确定初始状态
                    via[j][k]=1;
                    judgeh[j][x]=1;
                    judgel[k][x]=1;
                    int a=f(j);
                    int b=f(k);
                    judgex[a][b][x]=1;
                }
            }
        }
        dfs(1,1);
        print();
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值