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 ...
随机推荐
- 关于Onvif的event
昨天又仔细研究了一下camera的alarm功能,发现原来很简单,首先订阅一下,即create,拿到订阅号后直接pull,一旦收到信息就再次用订阅号pull.参考http://www.doc88.co ...
- vc++ mfc中拖动效果的实现 借助于CImageList
拖动是界面编程频繁使用的一个效果,在windows系统下可谓大行其道.纵观时下的应用软件几乎各个都支持各种各样拖动的效果,windows7更是把拖动做到了极致.其实说起来拖动的实现也很简单,对于有句柄 ...
- VS2013无法加载JSON格式
js中通过$.getJSON加载.json 文件时,报如下错误: 解决方案:dos命令中打开 IIS Express ,执行如下命令: appcmd set config /section:stat ...
- QPainter类的一些问题
QPainter painter1(this);//新建类 painter1.setRenderHint(QPainter::Antialiasing,true);//设置反锯齿 painter1.s ...
- spring的helloworld
前两天接到了支付宝的电话面试,问了我好多java中的知识,知识姐不看java好多年,这猛一问知道的东西还真是不少啊.趁着现在精力还算是比较旺盛,再把spring重新理一理,为下一次面试做准备. 学习每 ...
- WPF的"路径标记语法"
在此之前我们先了解一下WPF的"路径标记语法" M:表示绘制起点 // M 0,0 L:表示绘制直线 (H:横线 V:竖线) // L 100,0 C:三次方贝塞尔曲线 // ...
- Parallel线程使用
Parallel的静态For,ForEach和Invoke方法 在一些常见的编程情形中,使用任务也许会提升性能.为了简化编程,静态类System.Threading.Tasks.Paral ...
- VS使用过程中,编写JS没有智能提示解决方法
问题:编写基本Script代码没有问题,但是在编写DOM代码时候没有智能提示.也就是在编写一般javascript代码时候没有问题,但是要写DOM代码的时候发现没有智能提示,如document等都需要 ...
- cocos2d-x 观察者设计模式
1.参考文章 http://blog.csdn.net/vanquishedzxl/article/details/23616535 class HelloWorld : public cocos2 ...
- Nginx Location配置总结
Nginx Location配置总结 语法规则: location [=|~|~*|^~] /uri/ { - }= 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即 ...