题目链接: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. 关于onClick 提交数据问题

    我在添加 民工考勤表,用ajax 自动读取数据添加到相应模块之后 进行 OnClick="btnSubmit_Click" 保存,结果无法保存,之后我将光标锁定到某一个文本框内,就 ...

  2. POJ 1679 The Unique MST (最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 D ...

  3. 给定数组a[N]构造数组b[N]

    转自:http://blog.csdn.net/wumuzi520/article/details/7841280 给定一个数组a[N],我们希望构造数组b [N], 其中b[j]=a[0]*a[1] ...

  4. Hartree-Fock理论(更新中)

    预备知识: 基组 分子轨道基本概念与Hartree Product 平均场与Fock算符 在忽略分子中电子的相互作用时,我们有了一个粗糙的模型,虽然非常容易求解,但是描述的精确程度非常差. 考虑电子的 ...

  5. css 雪碧图的制作

    很多网站其实都用了雪碧图,确实方便了制作,以前以为这种小图标,都是一个一个图片呢(笑) 效果图如下: 代码如下: <html> <head lang="en"&g ...

  6. a标签中使用img后的高度多了4px

    前两天,在做一个网站的时候,发现a标签中使用img后的高度多了4px,各种纠结. 最后,仔细分析,终于找到原因了,因为img是行内元素,默认display: inline; 它与文本的默认行为类似,下 ...

  7. Table of Contents - Apache Commons

    Apache Commons 简述 CLI Usage of CLI Option Properties Codec 常见的编码解码 Compress Configuration2 Quick sta ...

  8. 纪念大一的日子,一个简单的C++

    //Author:xtyang //记得大一学C语言,永远都不明白如何调用一个函数,真是好可爱呀. #include<iostream> using namespace std; //定义 ...

  9. 前台JSP页面独立化

    一直从事Java WEB开发的过程中,当然要常常写JSP文件. 本人对JSP文件有些自己的想法. 页面要尽可能的简单,整洁,条理. js文件要尽可能地放到独立的js文件中,然后引用到当前的JSP文件中 ...

  10. 第一次使用easyUI

    一.项目结构图 二.在WebContent下新建resource文件夹,在resource底下创建easyui.将easyUI包放入其中. 三.在springMVC-servlet.xml写入资源路径 ...