题目链接: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. Oracle基础 数据库备份和恢复

    一.为什么需要数据备份 造成数据丢失的主要原因: 1.介质故障. 2.用户的错误操作. 3.服务器的彻底崩溃. 4.计算机病毒. 5.不可预料的因素. Oracle中故障类型分为以下4种. 1.语句故 ...

  2. Oracle 常用命令大全

    一.如何有效地利用数据字典 ORACLE的数据字典是数据库的重要组成部分之一,它随着数据库的产生而产生, 随着数据库的变化而变化, 体现为sys用户下的一些表和视图.数据字典名称是大写的英文字符. 数 ...

  3. pyenv的安装和使用

    1. 先安装crul和git sudo apt-get install curl git-core 2. 安装pyenv curl https://raw.github.com/yyuu/pyenv- ...

  4. Daily Query

    -- GI Report SELECT A.PLPKLNBR, D.DNDNHNBR, F.DNSAPCPO, C.PPPRODTE, A.GNUPDDTE GI_DATE, B.INHLDCDE, ...

  5. VS平台工具集版本

    参考:http://blog.csdn.net/hillseas/article/details/47373313 VS从2010之后开始支持使用之前的版本进行编译,可以在工程属性->常规中进行 ...

  6. Table of Contents - Apache Commons

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

  7. 根据不同的浏览器对不同元素进行css调整

    <!if firefox> .element { top:4px; } <![endif]> <!if chrome> .element { top:6px; } ...

  8. SQL 把查出来的信息整合为一张表

    select name ,population from bbc where name='France' union all select name ,population from bbc wher ...

  9. 简直喝血!H.265要被专利费活活玩死

    转自 http://news.mydrivers.com/1/440/440145.htm H.264是如今最流行的视频编码格式之一,不但技术先进,而且专利费很低,企业每年只需支付650万美元,而个人 ...

  10. 十九、android中判断sim卡状态和读取联系人资料的方法

    在写程序中,有时候可能需要获取sim卡中的一些联系人资料.在获取sim卡联系人前,我们一般会先判断sim卡状态,找到sim卡后再获取它的资料,如下代码我们可以读取sim卡中的联系人的一些信息. Pho ...