利用 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. 重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresenter

    原文:重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresente ...

  2. Redis整合Spring结合使用缓存实例(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的 ...

  3. port与大全portClose方法

    在网络技术,port(Port)通常,有两种含义:首先,物理意义port,例,ADSL Modem.枢纽.开关.路由器连接其他网络设备的接口,如RJ-45port.SCport等等.第二个是逻辑意义p ...

  4. 智能家居DIY

    近期智能家居比較火,将房子简单改造下,也算体验智能家居. 本文解说的是用无线的方式,长处是:不用改造现有线路,直接安装模块就可以实现想要的功能,花的钱也较少,共六百左右 =============== ...

  5. Nginx各版本的区别

    Nginx官网提供了三个类型的版本Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版Stable version:最新稳定版,生产环境上建议使用的版 ...

  6. DICOM医学图像处理:开源库mDCM与DCMTK的比較分析(一),JPEG无损压缩DCM图像

    背景介绍: 近期项目需求,须要使用C#进行最新的UI和相关DICOM3.0医学图像模块的开发.在C++语言下,我使用的是应用最广泛的DCMTK开源库,在本专栏的起初阶段的大多数博文都是对DCMTK开源 ...

  7. ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法

    主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 ...

  8. C#中的预编译指令介绍

    原文:C#中的预编译指令介绍 1.#define和#undef 用法: #define DEBUG #undef DEBUG #define告诉编译器,我定义了一个DEBUG的一个符号,他类似一个变量 ...

  9. 各种ESB产品比较(转)

    介绍了主流商业和开源ESB的发展趋势.可借鉴的地方和其缺点:         主要介绍:       Oracle Service Bus       WebSphere Message Broker ...

  10. 了解HTML5和“她”的 API (二)

    Communication(通信)     Cross Document Messaging (跨文档消息通信) postMessage API   origin security(源安全) chat ...