利用 Dancing Link 来解数独

详细的能够看    lrj 的训练指南 和 《 Dancing Links 在搜索中的应用 》这篇论文

Dancing Link 来求解数独 , 是通过求解精确覆盖

精确覆盖就是给出一个 01 矩阵 , 要求我们选择一些行 , 使得每一列有且仅有一个 1

对于数独问题 , 行就是我们的选择 , 即在第 i 行 第 j 列 放上 数字 k , 所以我们最多有 i * j * k 中选择

假设某些位置( x , y  )已经放了数字 a , 那么我们的选择仅仅能是 ( x , y , a ) , 否则的话, 我们能够选择 ( x , y , 1 ~ 9 )

对于列 , 也就是我们须要满足的条件 , 这里有 4 大类条件:

1. 第 x 行 第 y 列要有 数字

2. 第 x 行 要有 数字 y

3. 第 x 列 要有 数字 y

4. 第 x 个宫要有数字 y

所以总共同拥有 9*9*4 列 ( 如果 9*9*4 的数独的话 )

然后我们仅仅要进行DLX即可了

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = 9 * 9 * 4 + 10 ;
const int maxr = 9 * 9 * 9 + 10 ;
const int maxnode = maxr * 4 + maxr + 10 ; #define FOR( i , A , s ) for( int i = A[s] ; i != s ; i = A[i] ) struct DLX{
// maxn 列数 , maxnode 总节点数 , maxr 行数
int n , sz ;
int S[maxn] ; int row[maxnode] , col[maxnode] ;
int L[maxnode] , R[maxnode] , U[maxnode] , D[maxnode] ;
int H[maxr] ; int ansd , ans[maxr] ; void init( int N ) {
n = N ;
// 第一行的虚拟结点
for( int i = 0 ; i <= n ; i ++ ) {
U[i] = D[i] = i ;
L[i] = i - 1 ;
R[i] = i + 1 ;
}
R[n] = 0 ; L[0] = n ;
sz = n + 1 ;
// 每一列的个数
memset( S , 0 , sizeof(S) ) ;
// H[i] = -1 表示这一行还没有 1
// 否则表示第一个 1 的 sz 是多少
memset( H , -1 , sizeof(H)) ;
} // 在第r行第c列加入一个1
void Link( int r , int c ) {
row[sz] = r ;
col[sz] = c ;
S[c] ++ ; D[sz] = c ; U[sz] = U[c] ;
D[U[c]] = sz ; U[c] = sz ; if( H[r] < 0 ) { H[r] = L[sz] = R[sz] = sz ; }
else{
R[sz] = H[r] ;
L[sz] = L[H[r]] ;
R[L[sz]] = sz ;
L[R[sz]] = sz ;
} sz ++ ; } // 删除 第 c 列
void remove ( int c ) {
// 删除虚拟结点中的 c 列
L[R[c]] = L[c] ;
R[L[c]] = R[c] ;
// 从 c 列向下遍历
FOR( i , D , c ) {
// 删除遍历到的行
FOR( j , R , i ) {
D[U[j]] = D[j] ;
U[D[j]] = U[j] ;
-- S[col[j]] ;
}
}
} // 恢复第 c 列
void restore( int c ) {
FOR( i , U , c ) {
FOR( j , L , i ) {
++S[col[j]] ;
U[D[j]] = D[U[j]] = j ;
}
}
L[R[c]] = R[L[c]] = c ;
} bool dfs( int d ) {
// 假设已经没有列了 , 算法结束
if( R[0] == 0 ) {
ansd = d ;
return true ;
} // 找到 s 最小的列 , 加快搜索的速度
int c = R[0] ;
FOR( i , R , 0 ) {
if( S[i] < S[c] ) c = i ;
} // 删除第 c 列
remove( c ) ; // 遍历选中列中有1的行
FOR( i , D , c ) {
ans[d] = row[i] ;
// 删除选中行中有1的列
FOR( j , R , i ) {
remove( col[j] ) ;
}
if( dfs( d + 1 ) ) return true ;
// 回复删除掉的列
FOR( j , L , i ) {
restore( col[j] ) ;
}
}
restore( c ) ;
return false ;
} bool solve() {
if( !dfs(0) ) return false ;
return true ;
}
} dlx ; int ans[15][15] ; const int SLOT = 0 ;
const int ROW = 1 ;
const int COL = 2 ;
const int SUB = 3 ; inline int encode ( int a , int b , int c ) {
return a * 9 * 9 + b * 9 + c + 1 ;
} int main(){
char str[5] ;
int casn = 0 ;
while( scanf( "%s" , str ) != EOF ) { if( str[0] == '?' ) {
ans[0][0] = 0 ;
}else{
ans[0][0] = str[0] - '0' ;
}
for( int i = 0 ; i < 9 ; i ++ ) {
for( int j = 0 ; j < 9 ; j ++ ) {
if( i == 0 && j == 0 ) continue ;
scanf( "%s" , str ) ;
if( str[0] == '?' )
ans[i][j] = 0 ;
else
ans[i][j] = str[0] - '0' ;
}
} if( casn ++ ) {
puts( "" ) ;
} dlx.init( 9 * 9 * 4 ) ; for( int i = 0 ; i < 9 ; i ++ ) {
for( int j = 0 ; j < 9 ; j ++ ) {
for( int k = 0 ; k < 9 ; k ++ ) {
int rr = encode( i , j , k ) ;
if( ans[i][j] == 0 || ans[i][j] == k + 1 ) {
dlx.Link( rr , encode( SLOT , i , j ) ) ;
dlx.Link( rr , encode( ROW , i , k ) ) ;
dlx.Link( rr , encode( COL , j , k ) ) ;
dlx.Link( rr , encode( SUB , ( i / 3 ) * 3 + j / 3 , k ) ) ; }
}
}
} dlx.solve() ;
for( int i = 0 ; i < dlx.ansd ; i ++ ) {
int r , c , v ;
int t = dlx.ans[i] ;
t -- ;
v = t % 9 ;
t /= 9 ;
c = t % 9 ;
t /= 9 ;
r = t ;
ans[r][c] = v + 1 ;
}
for( int i = 0 ; i < 9 ; i ++ ) {
for( int j = 0 ; j < 9 ; j ++ ) {
printf( j == 8 ?"%d\n" : "%d " , ans[i][j] ) ;
}
}
}
return 0 ;
}

hdu 1426 Sudoku Killer ( Dancing Link 精确覆盖 )的更多相关文章

  1. HDU 1426 Sudoku Killer(dfs 解数独)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1426 Sudoku Killer Time Limit: 2000/1000 MS (Java/Oth ...

  2. hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. hdu 1426 Sudoku Killer (dfs)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. dancing link 精确覆盖 重复覆盖 (DLX)

    申明:因为转载的没有给出转载链接,我就把他的链接附上,请尊重原创: http://www.cnblogs.com/-sunshine/p/3358922.html 如果谁知道原创链接 给一下,请尊重原 ...

  5. hdu 1426 Sudoku Killer

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1426 #include<stdio.h> #include<math.h> #in ...

  6. HDU 1426 Sudoku Killer(搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1426 题意很明确,让你解一个9*9的数独. DFS即可. #include <cstdio> ...

  7. HDU 1426 Sudoku Killer【DFS 数独】

    自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品— ...

  8. HDU 1426 Sudoku Killer (回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398818 题意: 给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开.其中1-9代表该位 ...

  9. HDU 3111 Sudoku ( Dancing Links 精确覆盖模型 )

    推荐两篇学DLX的博文: http://bbs.9ria.com/thread-130295-1-1.html(这篇对DLX的工作过程演示的很详细) http://yzmduncan.iteye.co ...

随机推荐

  1. C# Windows Phone 8 WP8 , 文字超连结到网页 免打程式码,Xaml就搞定 ! !

    原文:C# Windows Phone 8 WP8 , 文字超连结到网页 免打程式码,Xaml就搞定 ! ! 一般我们在开发Windows Phone 8 APP ,有时会需要超连结连到其他的网页,但 ...

  2. C++习题 对象转换

    [Submit][Status][Web Board] Description 定义一个Teacher(教师)类(教师号,姓名,性别,薪金)和一个Student(学生)类(学号,姓名,性别,成绩),二 ...

  3. 冒泡排序算法(Java)

     冒泡排序即每次遍历.相邻数字间进行比較,前者大于后者进行交换,不断将最大值后移,直至沉至最后位置:算法关键要点在于确定每次循环的边界. 后面两种算法则是对冒泡排序一定程度上的改良,但相对于其它排 ...

  4. git tag使用

    #git tag command git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]       ...

  5. (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)

    称号: You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...

  6. hdu2993坡dp+二进制搜索

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. SendMail如何签名

    MailAddress类有两个参数 第1个参数:发送者的邮箱 第2个参数:发送者的签名 示例: MailMessage message = new MailMessage();message.From ...

  8. Matrix+POJ+二维树状数组初步

                                                                                                         ...

  9. 乐在其中设计模式(C#) - 外观模式(Facade Pattern)

    原文:乐在其中设计模式(C#) - 外观模式(Facade Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 外观模式(Facade Pattern) 作者:webabcd 介绍 ...

  10. UVa 11069 - A Graph Problem

    题目:给你一个集合{1,2,..,n},计算子集的个数,子集的元素不能相邻且不能再插入元素. 分析:dp,动态规划.相邻元素间仅仅能相差3或者2. 动态方程:f(k)= f(k-2)+ f(k-3): ...