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 ...
随机推荐
- ( 转)UVM验证方法学之一验证平台
在现代IC设计流程中,当设计人员根据设计规格说明书完成RTL代码之后,验证人员开始验证这些代码(通常称其为DUT,Design Under Test).验证工作主要保证从设计规格说明书到RTL转变的正 ...
- http://www.cnblogs.com/youring2/archive/2011/03/28/1997694.html
http://www.cnblogs.com/youring2/archive/2011/03/28/1997694.html
- [Eclipse] - eclipse集成jboss7.1
参考来源:http://www.cnblogs.com/HD/p/4000554.html 1.打开eclipse: 2.输入:jboss tools 本例已经安装,未安装的话点Install 3.只 ...
- shared memory realm does not exist
有天启动ORACLE,碰到如下问题 提示ORA-01034: ORACLE not available ORA-27101: shared memory realm does not exist 解决 ...
- 原生JS封装简单动画效果
原生JS封装简单动画效果 一致使用各种插件,有时候对原生JS陌生了起来,所以决定封装一个简单动画效果,熟悉JS原生代码 function animate(obj, target,num){ if(ob ...
- UML大战需求分析阅读笔记2
全面深入理解客户的业务,才能帮助我们准确的把握客户的需要.而在理解客户业务的同时,我们往往需要做业务流程再造(BPR:Business Process Reengineering)的工作.BPR简单说 ...
- 三表联查,这是我目前写过的最长的sql语句,嗯嗯,果然遇到问题才能让我更快成长,更复杂的语句也有了一些心得了
select sum(amount),sum(card_number) from sy_user inner join sy_admin on sy_user.customer_id=sy_admin ...
- python tm_isdst = -1, 0, 1 datetime module
Python 里的tm_isdst DST 是daylight saving time, 意思是:夏令时 在python的time, datetime模块下,按照struct_time格式输出时间,最 ...
- oracle exp imp 导入 正在跳过表 plsql 导入表 成功终止 数据 被导入
http://blog.csdn.net/agileclipse/article/details/12968011 .导入过程中,所有表导入都出现提示, 正在跳过表...某某表名 最后提示成功终止导入 ...
- svg path详解
svg的<path>标签具有强大的功能,主要包括以下命令 M(move to) 参数:x,y L(line to) 参数:x,y H 参数:x V 参数:y C S Q T Z 参考: