DLX (poj 3074)
题目:Sudoku
匪夷所思的方法,匪夷所思的速度!!!
https://github.com/ttlast/ACM/blob/master/Dancing%20Link%20DLX/poj%203074.cpp
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int inf = ;
int flag;
typedef long long LL;
#define FF(i,A,s) for(int i = A[s];i != s;i = A[i])
const int maxm = ;
const int maxn = ;
int used[maxn];
struct DLX{
int R[maxm],L[maxm],U[maxm],D[maxm];
int col[maxm],row[maxm];
int s[maxn];bool hash[maxn]; //重复覆盖
int pre,first,sz,NV,limit;
void init(int n) //列的数目
{
int i;
for(i = ;i <= n;i ++)
{
U[i] = i;D[i] = i;
col[i] = i; //
L[i] = i-,R[i] = i+;
}
NV = n;sz = n+;pre = -;first = ;
memset(s,,sizeof(s));
}
void insert(int i,int j) //一行一行的插入数据i行j列
{
if(i != pre) //pre表示前一行,如果不同,就更新前面那行的左右
{
R[sz-] = first;L[first] = sz -;
pre = i;first = sz;
}
L[sz] = sz - ;R[sz] = sz+; //可以将j列看做矩阵的最底部.
D[U[j]] = sz;
D[sz] = j;U[sz] = U[j];U[j] = sz;
row[sz] = i,col[sz] = j,s[j] ++;
sz ++;
}
void finish() { R[sz-] = first;L[first] = sz - ;}
void EXremove(int c){ //删除c列,而且与c有重复的行精确覆盖
L[R[c]] = L[c];R[L[c]] = R[c];
FF(i,D,c) FF(j,R,i) U[D[j]] = U[j],D[U[j]] = D[j],--s[col[j]];
}
void EXresume(int c){ //恢复c列,而且与c有重复的行
FF(i,U,c) FF(j,L,i) ++s[col[j]],U[D[j]] = j,D[U[j]] = j;
L[R[c]] = c;R[L[c]] = c;
}
//选择行,使每一列仅有一个
bool dfs(const int &k) //精确覆盖,选择了k行了。
{
if(flag) return true;
//if(k >= flag) return false;
if(R[] == )
{
//if(flag > k) flag = k;
flag = ;
return true;
}
//if(R[0] == 0) return true; //找到解
int idx = R[],i;
for(i = R[] ;i != ;i = R[i]) if(s[idx] > s[i]) idx = i;
EXremove(col[idx]);
FF(i,D,idx){
used[row[i]] = ;
FF(j,R,i) EXremove(col[j]);
if(dfs(k+)) return true; //查找下一个。
FF(j,L,i) EXresume(col[j]);
used[row[i]] = ;
}
EXresume(col[idx]);
return false;
}
//重复覆盖,选择最少行,所有的列被覆盖,后来添加的。
void remove(int & c) { FF(i,D,c) L[R[i]] = L[i],R[L[i]] = R[i]; } //去掉某列
void resume(int & c) { FF(i,U,c) L[R[i]] = i,R[L[i]] = i; }
int h(){ //f启发函数
int ret = ;
memset(hash,false,sizeof(hash));
for(int c = R[];c != ;c = R[c]) if(!hash[c]){ //可以修改c != 0部分
hash[c] = true; ret ++;
FF(i,D,c)
FF(j,R,i) hash[col[j]] = true;
}
return ret;
}
//重复覆盖,只删除列不删除行
bool dfs(const int & k,int & limit)
{
if(k+h() >= limit) return false;
if(R[] == )
{
if(k < limit) limit = k; return true;
}
int idx = R[],i;
for(i = R[] ;i != ;i = R[i]) if(s[idx] > s[i]) idx = i;
FF(i,D,idx){
remove(i);
FF(j,R,i) remove(j);
if(dfs(k+,limit)) return true;
FF(j,L,i) resume(j);
resume(i);
}
return false;
}
int astar() //or 二分。
{
limit = h();
while(!dfs(,limit)) limit ++; //修改点,limit最后期限
return limit;
}
};
DLX dlx;
char c[];
int map[];
int main()
{
int i,j,k,pos,row,cnt;
while(gets(c))
{
if(c[] == 'e') break;
dlx.init();
row = ;
memset(used,,sizeof(used));
for(i = ;i < ;i ++)
for(j = ;j < ;j ++)
{
if(c[i*+j] == '.'){
for(k = ;k <= ;k ++)
{
dlx.insert(row,i*+k); //行+数字
dlx.insert(row,+j*+k); //列+数
dlx.insert(row,+(i/+(j/)*)*+k); //小方块号号
dlx.insert(row,+i*+j+); //座位
map[row++] = k;
}
}else{
cnt = c[i*+j] - '';
dlx.insert(row,i*+cnt); //行+数字
dlx.insert(row,+j*+cnt); //列+数
dlx.insert(row,+(i/+(j/)*)*+cnt); //小方块号
dlx.insert(row,+i*+j+); //座位
map[row++] = cnt;
}
}
dlx.finish();
flag = ;
if(dlx.dfs())
{
for(i = ;i < row;i ++)
if(used[i]) printf("%d",map[i]);
printf("\n");
}else puts("NO");
}
return ;
}
DLX (poj 3074)的更多相关文章
- POJ 3074 Sudoku (DLX)
Sudoku Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- 搜索(DLX): POJ 3074 3076 Sudoku
POJ 3074 : Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller ...
- POJ 3074 Sudoku DLX精确覆盖
DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8336 Accepted: ...
- (简单) POJ 3074 Sudoku, DLX+精确覆盖。
Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgr ...
- POJ 3074 Sudoku (Dancing Links)
传送门:http://poj.org/problem?id=3074 DLX 数独的9*9的模板题. 具体建模详见下面这篇论文.其中9*9的数独怎么转化到精确覆盖问题,以及相关矩阵行列的定义都在下文中 ...
- 【POJ 3074】 Sudoku
[题目链接] http://poj.org/problem?id=3074 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 转化方法如下 : 我们知道,在一个数独中 : 1. ...
- POJ 3074 Sudoku(算竞进阶习题)
二进制优化+dfs 话说这题数据中真的丧心病狂..不加inline还过不去.. 因为不会DLX只好用二进制来优化了...万万没想到还是低空飘过 我们在行.列.格分别用一个9位二进制常数来记录什么数能放 ...
- POJ 3074 Sudoku (Dacing Links)
推荐一个写数独很好的博客:http://www.cnblogs.com/grenet/p/3163550.html 主要是把九宫格里的元素换到矩阵里面再求解dancing links 网上找的一模版 ...
- poj 3074 Sudoku(Dancing Links)
Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8152 Accepted: 2862 Descriptio ...
随机推荐
- makefile:2: *** 遗漏分隔符 。 停止
from http://hi.baidu.com/��֮��/blog/item/8ec00e2aca65a525d42af11b.html 我们在编写完makefile,然后在终端中 $make出现 ...
- Eclipse取消设置项目默认空间
分享一个小经验: 如果,在启动Eclipse时选中了Use this as the default and do not ask again 下次要启动时不会再次显示修改工作空间的选择, ...
- ng-style 的坑 - 对性能的影响
本文地址:http://www.cnblogs.com/jying/p/5633203.html 熟悉 angular 的前端对ng-style 一定不陌生,这个家伙可以绑定一个函数,使得我们可以在函 ...
- 只有一个radio的单选框如何在选中后取消选中
<input type="radio" id="all" value="1" name="executeRadio" ...
- Halcon学习之条形码实时扫描
dev_open_window(1,1,400,400,'blue',ThisHandle) create_bar_code_model([], [], BarCodeHandle) set_bar_ ...
- angular 调试 js (分 karms protractor / test e2e unit )
首页订阅 Protractor端到端的AngularJS测试框架教程 2014年01月18日 分类:教程, JavaScript, AngularJS Protractor是一个建立在WebDrive ...
- 使用Word发布文章到 WordPress 博客
使用Word发布文章到 WordPress 博客 我们都知道,WordPress 自带的编辑器功能比较弱,而使用 Word 编辑文档却功能强大.其实我们使用 Word 编辑好的文档也是可以直接发布到 ...
- 【补充版】HashMap(根据value筛选查找)
HashMap查找之根据Value查找 一般大家都知道对于HashMap而言都是通过key来进行查找.找到了key自然对应的value也就一并找到了.但是有些情况下就需要通过value来进行判断查找. ...
- 关于CMFCPropertyGridFontProperty的赋值问题
CMFCPropertyGridFontProperty是派生于CMFCPropertyGridProperty类的用于字体设置的类.它可以设置字体的名称.大小.粗细等各项参数.但是类并不提供用于初始 ...
- fedora23 tweak tool不工作解决方案
在启动器中打开 优化工具 失败 在终端中开启显示 Traceback (most recent call last): File "/usr/bin/gnome-tweak-tool&quo ...