POJ_P3074 Sudoku(DLX)

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9250 Accepted: 3332
Description

In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example,

. 2 7 3 8 . . 1 .
. 1 . . . 6 7 3 5
. . . . . . . 2 9
3 . 5 6 9 2 . 8 .
. . . . . . . . .
. 6 . 1 7 4 5 . 3
6 4 . . . . . . .
9 5 1 8 . . . 7 .
. 8 . . 6 5 3 4 .
Given some of the numbers in the grid, your goal is to determine the remaining numbers such that the numbers 1 through 9 appear exactly once in (1) each of nine 3 × 3 subgrids, (2) each of the nine rows, and (3) each of the nine columns.

Input

The input test file will contain multiple cases. Each test case consists of a single line containing 81 characters, which represent the 81 squares of the Sudoku grid, given one row at a time. Each character is either a digit (from 1 to 9) or a period (used to indicate an unfilled square). You may assume that each puzzle in the input will have exactly one solution. The end-of-file is denoted by a single line containing the word “end”.

Output

For each test case, print a line representing the completed Sudoku puzzle.

Sample Input

.2738..1..1…6735…….293.5692.8………..6.1745.364…….9518…7..8..6534.
……52..8.4……3…9…5.1…6..2..7……..3…..6…1……….7.4…….3.
end
Sample Output

527389416819426735436751829375692184194538267268174593643217958951843672782965341
416837529982465371735129468571298643293746185864351297647913852359682714128574936
Source

Stanford Local 2006

题解:
虽然暴力能过主要是用一下舞蹈链
首先关于数独有4个约束条件
1、每个格子只能填一个数字
2、每行每个数字只能填一遍
3、每列每个数字只能填一遍
4、每宫每个数字只能填一遍

那么就需要9*9*9*4列来搜索
具体实现传送门

ps:打错了一个字母调了好久,嘤嘤嘤~

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define N 9*9*9*9*9*4+10
#define M 9*9*9+10
struct DancingLink{
    int n,s,ansd;//列数 节点总数 
    int S[M],A[M],H[M];//S[]该列节点总数  A[]答案  H[]行首指针 
    int L[N],R[N],U[N],D[N]; //L[],R[],U[],D[] 上下左右 
    int C[N],X[N];//C[] X[] 行列编号 
    void init(int n){//初始化 
        this->n=n;
        for(int i=0;i<=n;i++)
            U[i]=i,D[i]=i,L[i]=i-1,R[i]=i+1;
        R[n]=0,L[0]=n;
        s=n+1;memset(S,0,sizeof(S));memset(H,-1,sizeof(H));
    }
    void DelCol(int c){//删除列 
        L[R[c]]=L[c];R[L[c]]=R[c];
        for(int i=D[c];i!=c;i=D[i])
            for(int j=R[i];j!=i;j=R[j])
                U[D[j]]=U[j],D[U[j]]=D[j],--S[C[j]];
    }
    void ResCol(int c){//恢复列 
        for(int i=U[c];i!=c;i=U[i])
            for(int j=L[i];j!=i;j=L[j])
                ++S[C[j]],U[D[j]]=j,D[U[j]]=j;
        L[R[c]]=c,R[L[c]]=c;
    }
    void AddNode(int r,int c){//添加节点 
        ++S[c],C[++s]=c,X[s]=r;
        D[s]=D[c],U[D[c]]=s,U[s]=c,D[c]=s;
        if(H[r]<0) H[r]=L[s]=R[s]=s;//行首节点
        else  R[s]=R[H[r]],L[R[H[r]]]=s,L[s]=H[r],R[H[r]]=s;
    }
    bool dfs(int d){//深度,深搜遍历 
        if(!R[0]){
            ansd=d;return true;
        }
        int c=R[0];
        for(int i=R[0];i;i=R[i]) if(S[i]<S[c]) c=i;
        DelCol(c);
        for(int i=D[c];i!=c;i=D[i]){
            A[d]=i;
            for(int j=R[i];j!=i;j=R[j]) DelCol(C[j]);
            if(dfs(d+1)) return true;
            for(int j=L[i];j!=i;j=L[j]) ResCol(C[j]);
        }
        ResCol(c);return false;
    }
    void calc(int &r,int &c1,int &c2,int &c3,int &c4,int i,int j,int k){ 
        const int P=9; 
        r=(i*P+j)*P+k,c1=i*P+j+1,c2=P*P+i*P+k,c3=P*P*2+j*P+k,c4=P*P*3+((i/3)*3+(j/3))*P+k;  
    }  
    void solve(){
        char ch[M];int c1,c2,c3,c4,r;
        while(scanf("%s",ch)!=EOF&&ch[0]!='e'){
            init(9*9*4);
            for(int k=0,i=0;i<9;i++)
                for(int j=0;j<9;j++,k++)
                    if(ch[k]>'0'&&ch[k]<='9'){
                        calc(r,c1,c2,c3,c4,i,j,ch[k]-'0');
                        AddNode(r,c1);AddNode(r,c2);
                        AddNode(r,c3);AddNode(r,c4);
                    }
                    else{
                        for(int p=1;p<=9;p++){
                            calc(r,c1,c2,c3,c4,i,j,p);
                            AddNode(r,c1);AddNode(r,c2);
                            AddNode(r,c3);AddNode(r,c4);
                        }
                    }
            if(dfs(0)){
                for(int i=0;i<ansd;i++) ch[(X[A[i]]-1)/9+1]=(X[A[i]]-1)%9+1;  
                for(int i=1;i<=9*9;i++) printf("%d",ch[i]); 
                putchar('\n');
            }
        } 
    } 

}DLX; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值