POJ - 2676 Sudoku dfs

3 篇文章 0 订阅

Sudoku

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

题意:
解数独。
我以前做过这道题,当时是用DLX做的…
dfs写了两种方法【其实只有一种,但check的方式不一样。
忘了清零wa了一发。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 10;

int n;
int a[N][N];
/*
bool check(int pos,int num){
    int line=(pos-1)/9+1,row=pos%9;if(row==0) row=9;
    for(int i=1;i<=9;++i) if(a[line][i]==num) return false;
    for(int i=1;i<=9;++i) if(a[i][row]==num) return false;
    if(line<=3) line=1;
    else if(line<=6) line=4;
    else line=7;
    if(row<=3) row=1;
    else if(row<=6) row=4;
    else row=7;
    for(int i=line;i<=line+2;++i)
        for(int j=row;j<=row+2;++j)
            if(a[i][j]==num) return false;
    return true;
}*/
//朴素的check 

int b[N][N],c[N][N],d[N][N];
//b[i][j]表示第i行j这种数字是否出现过
//c[i][j]表示第i列j这种数字是否出现过 
//d[i][j]表示第i个小九宫格中j这种数字是否出现过 
bool flag=false;

int getnum(int i,int j){
    return (i-1)/3*3+(j-1)/3+1;
}
/*
void dfs(int x){
    if(x>81||flag) {flag=true;return ;}
    if(flag==true) return ;
    int line=(x-1)/9+1,row=x%9;
    if(row==0) row=9;
    if(a[line][row]) dfs(x+1);
    else{
        for(int i=1;i<=9;++i){
            if(check(x,i)) {
                a[line][row]=i;
                dfs(x+1);
                if(flag) return ;
                a[line][row]=0;
            }
        }
    }
}*/

void dfs1(int pos){
    if(pos>81||flag) {flag=true;return ;}
    int line=(pos-1)/9+1,row=pos%9;
    if(row==0) row=9;
    if(a[line][row]) dfs1(pos+1);
    else{
        int k=getnum(line,row);
        for(int i=1;i<=9;++i){
            if(b[line][i]==0&&c[row][i]==0&&d[k][i]==0){
                a[line][row]=i;
                b[line][i]=c[row][i]=d[k][i]=1;
                dfs1(pos+1);
                if(flag) return ;
                b[line][i]=a[line][row]=c[row][i]=d[k][i]=0;
            }
        }
    }
}

#define ms(x,y) memset(x,y,sizeof(x))
void update(){
    ms(b,0),ms(c,0),ms(d,0);
}

int main(){
    scanf("%d",&n);
    while(n--){
        update();
        for(int i=1;i<=9;++i){
            for(int j=1;j<=9;++j){
                scanf("%1d",&a[i][j]);
                b[i][a[i][j]]=1;
                c[j][a[i][j]]=1;
                d[getnum(i,j)][a[i][j]]=1;
            }
        }
        flag=false;
        dfs1(1);
        if(flag==true){
            for(int i=1;i<=9;++i){
                for(int j=1;j<=9;++j)
                    printf("%d",a[i][j]);
                printf("\n");
            }
        }
        else printf("impossible!\n");
    }
    return 0;
}

居然找到了以前的代码。
当时好像还不喜欢写博客来着。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int const maxn = 10010;

int n,m,inf1=81,inf2=162,inf3=243,inf4=324;
int t;
char s[10][10];
int G[10][10];

struct DLX{
    int n,m,size;
    int U[maxn],D[maxn],R[maxn],L[maxn],col[maxn],row[maxn];
    int H[maxn];//行头结点 
    int S[maxn];//列头结点
    int ansd,ans[maxn];

    void in_it(int _n){
        n=_n;
        memset(H,-1,sizeof(H));
        for(int i=0;i<=n;i++){
            S[i]=0;
            U[i]=i;
            D[i]=i;//初始状态下,上下指向自己
            R[i]=i+1;//右边的指针指向右边一个数 
            L[i]=i-1;//左边的指针指向左边一个数
        }
        R[n]=0;L[0]=n;//最左边和最右边的数应该是连起来的,所以左指向最后一个数,最右一个数指向最左边
        size=n;//编号,每一列都应该有一个头结点,编号1-m
    }//附初始值

    void link(int r,int l){//将第r行,第l列的节点加入 
        size++;
        S[l]++;//第size个节点所在的列为l,则当前列的节点数++;(注意:size是从m开始编号的)
        row[size]=r;//第size个节点的行位置为r        
        col[size]=l;
        U[size]=U[l];//这个节点上面的指针就指向这一列      
        D[size]=l;//当前节点的下面的指针指向当前这一列所能指向的最低位置
        D[U[l]]=size;//当前这一列的最低位置指向该节点      
        U[l]=size;//当前这一列的最低位置所能指向的最高位置就是这个节点 
        if(~H[r]){//如果这个点是这一行最先加入的节点 
            L[size]=L[H[r]];
            R[size]=H[r];//左右的指针都指向这个节点 
            L[R[size]]=size;
            R[L[size]]=size;
        }
        else  {  
              H[r]=L[size]=R[size]=size; 
        }  
    }

    void remove(int c){//删除节点c,以及c上下节点所在的行,每次调用这个函数,都是从列头节点开始向下删除,固c也可以理解为第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[col[j]]--;//节点数减小 
            }
        }
    }
    void resume(int c){//恢复节点c
        for(int i=U[c];i!=c;i=U[i]){
            for(int j=L[i];j!=i;j=L[j]){
                S[col[j]]++;                
                D[U[j]]=j;
                U[D[j]]=j;
            }
        }//直接将左右上下节点掠过这个点连旁边的点就算删除了
        R[L[c]]=c;          
        L[R[c]]=c;
    }
    bool dance(int d){//递归深度
        if(R[0]==0){//已经没有元素未被覆盖了 
            ansd=d;
            return true;
        }
        int c=R[0];
        for(int i=R[0];i!=0;i=R[i])  
            if(S[i]<S[c])  
            c=i;
        remove(c);//找到节点数最少的列,当前元素不是原图上0,1的节点,而是列头节点
        for(int i=D[c];i!=c;i=D[i]){
            ans[d]=row[i];//列头结点下面的一个节点
            for(int j=R[i];j!=i;j=R[j])  
                remove(col[j]);  
            if(dance(d+1))//找到,返回  
                return true;  
            for(int j=L[i];j!=i;j=L[j])//没有找到,删除这个节点,重新找  
                resume(col[j]);      
        }
        resume(c);
        return false;
    }

void build(){
    for(int i=0;i<9;i++){
        scanf("%s",s[i]);
    }
    for(int i=0;i<9;i++){
        for(int j=0;j<9;j++){
            G[i][j]=s[i][j]-'0';
        }
    }
    for(int i=0;i<9;i++){
        for(int j=0;j<9;j++){
            if(G[i][j]!=0){
                int r=(i*9+j)*9+G[i][j];  
                int c1=i*9+j+1;  
                int c2=inf1+i*9+G[i][j];  
                int c3=inf2+j*9+G[i][j];  
                int c4=inf3+((i/3)*3+(j/3))*9+G[i][j];  
                link(r,c1);  
                link(r,c2);  
                link(r,c3);  
                link(r,c4);  
                S[c1]=-1;
                S[c2]=-1;
                S[c3]=-1;
                S[c4]=-1;  
            }
        }
    }
    for(int i=0;i<9;i++){
        for(int j=0;j<9;j++){
            if(G[i][j]==0){
                for(int k=1;k<=9;k++){
                  int r=(i*9+j)*9+k;  
                  int c1=i*9+j+1;  
                  int c2=inf1+i*9+k;  
                  int c3=inf2+j*9+k;  
                  int c4=inf3+((i/3)*3+(j/3))*9+k;
                  if(~S[c1]&&~S[c2]&&~S[c3]&&~S[c4]){
                  link(r,c1);  
                  link(r,c2);  
                  link(r,c3);  
                  link(r,c4);  
                  }
                }
            }
        }
    }   
}   
}x;

int main(){
    scanf("%d",&t);
    while(t--){
       x.in_it(324);
       x.build();
       x.dance(0);
       for(int i=0;i<x.ansd;i++){
            int key=(x.ans[i]-1)%9+1;  
            int z=(x.ans[i]-1)/9/9+1;  
            int y=(x.ans[i]-1)/9%9+1;  
            G[z][y] = key ;  
         }
       printf("\n");
         for(int i=1;i<=9;i++){
            for(int j=1;j<=9;j++){
                printf("%d",G[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值