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

Description

Given a M×N matrix AAij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1.

Input

There are multiple cases ended by EOF. Test case up to 500.The first line of input is MN (M ≤ 16, N ≤ 300). The next M lines every line contains N integers separated by space.

Output

For each test case, if you could find it output "Yes, I found it", otherwise output "It is impossible" per line.

Sample Input

3 3
0 1 0
0 0 1
1 0 0
4 4
0 0 0 1
1 0 0 0
1 1 0 1
0 1 0 0

Sample Output

Yes, I found it
It is impossible

Source

题意:

给你一个n*m(n<=16,m<=300)的矩阵。矩阵的每一个元素仅仅能是0或1.如今问你能不能从里面选一些列出来使的没一列有且仅有一个1.

思路:

開始逗比了。把题看成每行仅仅有16个。一看16就乐了。这是典型的壮压嘛。

把每行压成长度为16的二进制数。

然后就是可行性判定了。写完一交re了。

这还用说。

m,n都看错了不re才怪。因为每行值个数太多。不能壮压了。无赖。仅仅能百度。得知是Dancing Links.于是百度其资料。

链接一枚

具体见代码:

#include<iostream>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=6010;
int U[maxn],D[maxn],L[maxn],R[maxn],C[maxn];//上下左右指针。 c[i]结点i相应的列。
int S[310],H[310];//S[i]为i列1的个数。H[i]为i行的尾指针。 int n,m,cnt;
void init()
{
int i;
for(i=1;i<=n;i++)
H[i]=-1;
for(i=0;i<=m;i++)
{
S[i]=0;
L[i+1]=i;
R[i]=i+1;
U[i]=D[i]=i;
}
R[m]=0;
cnt=m+1;
}
void Insert(int r,int c)
{ //头插法建链表
U[cnt]=c,D[cnt]=D[c];//确定新增结点上下指针信息
U[D[c]]=cnt,D[c]=cnt;//恢复链表信息
if(H[r]==-1) //确定左右指针信息
H[r]=L[cnt]=R[cnt]=cnt;//增加头
else
{
L[cnt]=H[r],R[cnt]=R[H[r]];//头插法
L[R[H[r]]]=cnt,R[H[r]]=cnt;
}
S[c]++;//更新附加信息
C[cnt++]=c;
}
void Remove(int c)//移除c列。 {
int i,j;
R[L[c]]=R[c],L[R[c]]=L[c];
for(i=D[c];i!=c;i=D[i])
for(j=R[i];j!=i;j=R[j])
D[U[j]]=D[j],U[D[j]]=U[j],S[C[j]]--;
}
void Resume(int c)//还原c列。 {
int i,j;
R[L[c]]=L[R[c]]=c;
for(i=D[c];i!=c;i=D[i])
for(j=R[i];j!=i;j=R[j])
D[U[j]]=U[D[j]]=j,S[C[j]]++;
}
bool dfs()
{
if(R[0]==0)
return true;
int i,j,c,miv=INF;
for(i=R[0];i;i=R[i])
if(S[i]<miv)
miv=S[i],c=i;
Remove(c);//处理第c列
for(i=D[c];i!=c;i=D[i])
{
for(j=R[i];j!=i;j=R[j])
Remove(C[j]);
if(dfs())
return true;
for(j=L[i];j!=i;j=L[j])
Resume(C[j]);
}
Resume(c);
return false;
}
int main()
{
int op,i,j;
while(~scanf("%d%d",&n,&m))
{
init();
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
scanf("%d",&op);
if(op)
Insert(i,j);
}
if(dfs())
printf("Yes, I found it\n");
else
printf("It is impossible\n");
}
return 0;
}

poj 3740 Easy Finding(Dancing Links)的更多相关文章

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

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

  2. poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析

    题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...

  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. POJ 3074 Sudoku (Dancing Links)

    传送门:http://poj.org/problem?id=3074 DLX 数独的9*9的模板题. 具体建模详见下面这篇论文.其中9*9的数独怎么转化到精确覆盖问题,以及相关矩阵行列的定义都在下文中 ...

  7. Easy Finding POJ - 3740 (DLX)

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

  8. 【POJ 3740】 Easy Finding

    [题目链接] http://poj.org/problem?id=3740 [算法] Dancing Links算法解精确覆盖问题 详见这篇文章 : https://www.cnblogs.com/g ...

  9. POJ 3740 Dancing Links

    Dancing Links学习:http://www.cnblogs.com/steady/archive/2011/03/15/1984791.html 以及图文学习:http://www.cnbl ...

随机推荐

  1. js获取当前页面url网址等信息

    使用js获取当前页面的url网址信息. 1.设置或获取整个 URL 为字符串: window.location.href 2.设置或获取与 URL 关联的端口号码: window.location.p ...

  2. DPDK

    Intel DPDK 全面解读   高性能网络技术 随着云计算产业的异军突起,网络技术的不断创新,越来越多的网络设备基础架构逐步向基于通用处理器平台的架构方向融合,从传统的物理网络到虚拟网络,从扁平化 ...

  3. SVN 版本服务器搭配全过程详解(含服务端、客户端)

    1.为什么要用VisualSVN Server,而不用Subversion? 回答: 因为如果直接使用Subversion,那么在Windows 系统上,要想让它随系统启动,就要封装SVN Serve ...

  4. gitlab yum 源

    https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/

  5. rpm -e 包名 卸载安装的二进制包

    rpm -e --nodeps nc-.el6.x86_64 #--nodeps 不包含依赖包,直接删除rpm包

  6. MAC下cmake安装

    可以参考网上手动下载cmake的源码包进行安装,http://www.cmake.org/download/ 解压后运行sudo ./bootstrap && sudo make &a ...

  7. 让一个div始终固定在页面的某一固定位置的方法

    方法一:直接用position:fixed 方法二:写一个滚动条滚动事件,让这个div设置 position:absolute 该top的距离等于滚动的距离scrollTop() 写法如下:$(win ...

  8. java 多线程4: java线程的优先级

    Java线程的优先级取值范围是1 (Thread.MIN_PRIORITY ) 到 10 (Thread.MAX_PRIORITY ).如果没有设置, 线程默认的优先级是NORM_PRIORITY.这 ...

  9. 完成wamp安装后Mysql配置记录

    注意:下面如没有特殊说明,默认都是修改my.ini配置,修改完配置需要重启mysql服务. 1.安装完wamp后输入命令有乱码提示: 运行mysql命令时,出现的错误提示是乱码 :    ERROR ...

  10. 在chrome中的source找不到自己写的js时处理方法

    今天准备调试下js,突然发现在谷歌的中source中找不到我要调试的js,后来查资料和听同事说谷歌会把js压缩,于是找到以下方法来调试js 1. debugger神器 横扫各个浏览器.脚本运行到deb ...