POJ 2676-Sudoku(DFS-数独)

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


题目意思:

数独游戏,每行每列数字不同,9个3*3的方块中数字不同,给出9*9的数字,0表示为空需要自己填入数字,求出一组解法。

解题思路:

DFS暴搜…
这题有毒,反着搜比正着搜快,估计是数据的缘故,有的大神正搜也妥妥的A了,我这个渣渣正搜TLE反搜32MS过的…
先分别用结构体和数组保存81个数的值及其坐标,从80~0反搜来DFS,如果当前位是0则依次尝试填入1~9,每填一个就检查一下是否当前3*3的方块和每行每列均不重复,如果不重复则能填入,继续搜索下一位上的数;如果后面无法继续填入,那么需要回溯,还原为0。直到搜索完81个位置上的数,仅输出一组解,所以要做个标记跳出。
9个3*3的方块我是直接根据行列范围暴力确定的……

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 5050
struct Node
{
    int x,y,v;
} b[81];
int a[9][9];
bool flag;
bool check(int m,int n)
{
    if(m>=0&&m<=2&&n>=0&&n<=2)//0
        for(int i=0; i<=2; ++i)
            for(int j=0; j<=2; ++j)
                if(i!=m&&j!=n)
                    if(a[i][j]==a[m][n])
                        return false;
    if(m>=0&&m<=2&&n>=3&&n<=5)//1
        for(int i=0; i<=2; ++i)
            for(int j=3; j<=5; ++j)
                if(i!=m&&j!=n)
                    if(a[i][j]==a[m][n])
                        return false;
    if(m>=0&&m<=2&&n>=6&&n<=8)//2
        for(int i=0; i<=2; ++i)
            for(int j=6; j<=8; ++j)
                if(i!=m&&j!=n)
                    if(a[i][j]==a[m][n])
                        return false;
    if(m>=3&&m<=5&&n>=0&&n<=2)//3
        for(int i=3; i<=5; ++i)
            for(int j=0; j<=2; ++j)
                if(i!=m&&j!=n)
                    if(a[i][j]==a[m][n])
                        return false;
    if(m>=3&&m<=5&&n>=3&&n<=5)//4
        for(int i=3; i<=5; ++i)
            for(int j=3; j<=5; ++j)
                if(i!=m&&j!=n)
                    if(a[i][j]==a[m][n])
                        return false;
    if(m>=3&&m<=5&&n>=6&&n<=8)//5
        for(int i=3; i<=5; ++i)
            for(int j=6; j<=8; ++j)
                if(i!=m&&j!=n)
                    if(a[i][j]==a[m][n])
                        return false;
    if(m>=6&&m<=8&&n>=0&&n<=2)//6
        for(int i=6; i<=8; ++i)
            for(int j=0; j<=2; ++j)
                if(i!=m&&j!=n)
                    if(a[i][j]==a[m][n])
                        return false;
    if(m>=6&&m<=8&&n>=3&&n<=5)//7
        for(int i=6; i<=8; ++i)
            for(int j=3; j<=5; ++j)
                if(i!=m&&j!=n)
                    if(a[i][j]==a[m][n])
                        return false;
    if(m>=6&&m<=8&&n>=6&&n<=8)//8
        for(int i=6; i<=8; ++i)
            for(int j=6; j<=8; ++j)
                if(i!=m&&j!=n)
                    if(a[i][j]==a[m][n])
                        return false;
    for(int i=m+1; i<9; ++i)  if(a[i][n]==a[m][n]) return false;
    for(int i=m-1; i>=0; --i) if(a[i][n]==a[m][n])return false;
    for(int i=n+1; i<9; ++i)  if(a[m][i]==a[m][n])return false;
    for(int i=n-1; i>=0; --i) if(a[m][i]==a[m][n])return false;
    return true;
}
void dfs(int d)
{
    if(flag)  return;//保证只输出一组
    if(d<0)
    {
        for(int i=0; i<9; ++i)
        {
            for(int j=0; j<9; ++j)
                cout<<a[i][j];
            cout<<endl;
        }
        flag=true;
        return;
    }
    if(b[d].v==0)//需要填入数字
        for(int k=1; k<=9; ++k)//尝试填入1~9
        {
            b[d].v=k;
            a[b[d].x][b[d].y]=k;
            if(check(b[d].x,b[d].y))//可以填入
                dfs(d-1);
            b[d].v=0;//还原为0
            a[b[d].x][b[d].y]=0;
        }
    else dfs(d-1);//不需要填入数字
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("F:/cb/read.txt","r",stdin);
    //freopen("F:/cb/out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        flag=false;
        int cnt=0;
        string s;
        for(int i=0; i<9; ++i)
        {
            cin>>s;
            for(int j=0; j<9; ++j)
            {
                b[cnt].x=i,b[cnt].y=j;
                b[cnt++].v=s[j]-'0';
                a[i][j]=s[j]-'0';
            }
        }
        dfs(80);//反搜
    }
    return 0;
}
/*
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值