题目链接:http://poj.org/problem?id=3740

题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1?

原矩阵N*M $ 1<= N <= 16 $ , $ 1 <= M <= 300$

解法1:由于行数不多,二进制枚举选出的行数,时间复杂度为O((1<<16)*K), 其中K即为判断选出的行数是否存在相同的列中有重复的1;

优化:将1状压即可,这样300的列值,压缩在int的32位中,使得列数“好像”小于10了;这样每次只需要 一个数 进行与运算,即可判断32个列是否有重复的1;使得K <= 10;

并且在选中一列和删除一列时, 两次 xor操作就可复原~~

47ms ~~

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; const int bit = ;
const int L = ;
int n, m, x[][L], g[L], w[L];
bool check(int *x, int *w)
{
for(int i = ; i < L; i++){
if(x[i] & w[i]) return true;
}
return false;
}
bool dfs(int p)
{
int flag = ;
for(int i = ; i < L && flag; i++) if(g[i] != w[i]) flag = ;
if(flag) return true;
if(p == n) return false;
if(check(x[p], w)) return dfs(p + ); // 判断是否当前的列与之前选中的列有重复的1 for(int i = ; i < L; i++) w[i] ^= x[p][i];
if(dfs(p+)) return true;
for(int i = ; i < L; i++) w[i] ^= x[p][i]; // 不选当前的列
return dfs(p+);
}
int main()
{
char a;
while( scanf("%d%d", &n, &m) == ){
memset(x, , sizeof(x));
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
a = getchar();
while (!isdigit(a)) a = getchar();
if (a == '') x[i][j/bit] |= << j%bit;
}
}
memset(w, , sizeof(w));
memset(g, , sizeof(g));
for(int i = ; i < m; i++) g[i/bit] |= << i%bit; if(dfs()) puts("Yes, I found it");
else puts("It is impossible");
}
}

解法2: DLX模板题,初学DLX,下面写下理解:

在DLX中搞清楚几个函数的含义,以及删除和恢复列操作的含义基本上理解了DLX 的模板了;

C[i] 表示每个有效节点所在的列, S[i] 表示每一列有效节点的个数,H[i] 表示每一列最右边有效节点的的标号;

L[i], R[i], U[i], D[i] 分别表示有效节点 i 上下左右节点的标号;

其中U[c] (c为列的标号) 表示列c中最下面的有效节点的标号, D[c] (c为列的标号)表示列c中最上面的有效节点的标号;

并且在删除和恢复一列时,循环遍历的是该列中的每一个节点; 这和选中一列,之后删除/恢复 行不同, 删除/恢复 行并没有包括起点(起点在列中操作了);

具体的DLX思路: 选出S中最小的列c, 删除列c中所有的有效节点所在的行,之后枚举选出的是哪行;再删除该行所有有效节点所在的列;递归求解即可;

63ms ... 进竟然没有压缩枚举快,,写挫了

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = ;
int tot, head, n, m;
int C[maxn], S[maxn], H[maxn];
int U[maxn], D[maxn], L[maxn], R[maxn];
inline void add_link(int i, int j)
{
C[++tot] = j;
S[j]++; D[tot] = j;
U[tot] = U[j];
D[U[tot]] = tot;
U[D[tot]] = tot;//U[j]表示第j列最下面节点标号; if(H[i]) L[tot] = H[i], R[tot] = R[H[i]]; // H[i]是第i行最右边的节点标号
else R[tot] = L[tot] = tot; R[L[tot]] = tot;
L[R[tot]] = tot;
H[i] = tot;
} void Remove(int c)
{
R[L[c]] = R[c]; L[R[c]] = L[c];
for(int i = D[c]; i != c ; i = D[i]){
for(int j = R[i]; j != i; j = R[j]){
U[D[j]] = U[j];
D[U[j]] = D[j];
S[C[j]]--;
}
}
} void Resume(int c)
{
for(int i = U[c]; i != c; i = U[i]){
for(int j = L[i]; j != i; j = L[j]){
U[D[j]] = j;
D[U[j]] = j;
S[C[j]]++;
}
}
R[L[c]] = c; L[R[c]] = c;
}
bool DLX()
{
if(R[head] == head) return true;
int c, mn = inf;
for(int i = R[head]; i != head; i = R[i]) if(mn > S[i]) c = i, mn = S[i]; Remove(c);
for(int i = D[c]; i != c; i = D[i]){
for(int j = R[i]; j != i; j = R[j]) Remove(C[j]); if(DLX()) return true;
for(int j = L[i]; j != i; j = L[j]) Resume(C[j]);
}
Resume(c); return false;
}
int main()
{
while( scanf("%d%d", &n, &m) == ){
tot = m; head = ;
for(int i = ; i <= m; i++){
C[i] = U[i] = D[i] = i;
L[i+] = i;
R[i] = i+;
S[i] = ;
}
L[] = m, R[m] = ; char x;
for(int i = ; i <= n; i++){
H[i] = ;
for(int j = ; j <= m; j++){
x = getchar();
while (!isdigit(x)) x = getchar();
if (x == '') add_link(i, j);
}
}
if(DLX()) puts("Yes, I found it");
else puts("It is impossible");
}
}

poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析的更多相关文章

  1. poj 3740 Easy Finding(Dancing Links)

    Easy Finding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15668   Accepted: 4163 Des ...

  2. [ACM] POJ 3740 Easy Finding (DLX模板题)

    Easy Finding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16178   Accepted: 4343 Des ...

  3. [ACM] POJ 3740 Easy Finding (DFS)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16202   Accepted: 4349 Description Give ...

  4. poj 3740 Easy Finding 精确匹配

    题目链接 dlx的第一题, 真是坎坷..... #include <iostream> #include <vector> #include <cstdio> #i ...

  5. POJ 3740 Easy Finding

    #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...

  6. Easy Finding POJ - 3740 (DLX)

    显然这是一道dfs简单题 或许匹配也能做 然而用了dancing links 显然这也是一道模板题 好的吧 调了一上午 终于弄好了模板 Easy Finding Time Limit: 1000MS ...

  7. POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】

    题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...

  8. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  9. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

随机推荐

  1. Hadoop学习笔记(3)——分布式环境搭建

    Hadoop学习笔记(3) ——分布式环境搭建 前面,我们已经在单机上把Hadoop运行起来了,但我们知道Hadoop支持分布式的,而它的优点就是在分布上突出的,所以我们得搭个环境模拟一下. 在这里, ...

  2. NS_ENUM和NS_OPTIONS区别

    首先,NS_ENUM和NS_OPTIONS都是宏. Foundation框架中定义了一些辅助的宏,用这些宏来定义枚举类型时,也可以指定用于保存枚举值的底层数据类型.这些宏具有向后兼容能力,如果目标平台 ...

  3. USB HID 协议入门

    转载请注明来源:cuixiaolei的技术博客 USB HID设备类的应用场合 USB HID类是USB设备的一个标准设备类,包括的设备非常多.HID类设备定义它属于人机交互操作的设备,用于控制计算机 ...

  4. 重构24-Remove Arrowhead Antipattern(去掉箭头反模式)

    基于c2的wiki条目.Los Techies的Chris Missal同样也些了一篇关于反模式的post.  简单地说,当你使用大量的嵌套条件判断时,形成了箭头型的代码,这就是箭头反模式(arrow ...

  5. Windows8不联网直接安装.Net 3.5 Framework的方法

    把你的系统ISO加载到虚拟光驱或插入系统安装盘,找到X:\sources\sxs路径(X是你的光驱盘符).输入下面命令,盘符以D盘为例DISM /Online /Enable-Feature /Fea ...

  6. Miniui updateRow更改列字段值

    当UPC等于upccode时,更改列Scanned+1 //先grid.findRow找到UPC等于upccode的行对象 var row = grid.findRow(function (row) ...

  7. ASP.NET MVC 4 SimpleMembership Provider (1)

    新的ASP.NET MVC 4.0 提供了一个新的Membership Provider,叫SimpleMembership. 首先,我们建立一个新的solution 首先我们先看一下web.conf ...

  8. 【转】c语言入门教程 / c语言入门经典书籍

    用C语言开始编写代码 初级:C语言入门必备 (以下两本书任选一本即可) C语言是作为从事实际编程工作的程序员的一种工具而出现的,本阶段的学习最主要的目的就是尽快掌握如何用c语言编写程序的技能.对c语言 ...

  9. 解决方案:Default Activity Not Found !

    在Android Studio 中按 Ctrl+F11运行工程的时候,突然弹出一个Edit Configuration 对话框,让选择一个默认的Activity,之前默认情况下是直接编译到虚拟机运行的 ...

  10. php 读写xml 修改xml

    需要修改的xml <?xml version="1.0"?> <config> <administration> <adminuser&g ...