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

Description

Given a M×N matrix A. Aij ∈ {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
M, N (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

题意为:

给定由01构成的矩阵,问能不能选出几行构成新矩阵,使得新矩阵每列有且仅仅有一个1.

枚举全部行,行号递增,推断每行能否够选(是否与前面所选的行发生冲突)。当前行可选时。第j列假设为1。则用vis[j]=1标记,当行号>n(行数)时。推断每列是否都有1.

#include <iostream>
#include <stdio.h>
#include <string.h>
const int maxn=18;
const int maxm=310;
int mp[maxn][maxm];
bool vis[maxm];
int n,m;
bool yes;
using namespace std; bool row_ok(int rth)//rth为行号,推断第rth行能够选
{
for(int j=1;j<=m;++j)
if(vis[j]&&mp[rth][j])//第j列已经有1了
return false;
for(int j=1;j<=m;++j)//能够选
if(mp[rth][j])
vis[j]=true;
return true;
} bool judge()//当选的行号大于n时。推断一下是不是每列都有1
{
for(int j=1;j<=m;++j)
if(!vis[j])
return false;
return true;
} void dfs(int rth)
{
if(rth>n+1)//由于当rth=n+1时,还须要推断judge()
return;
if(judge())//注意这两个if
{
yes=1;
return;
}
for(int i=rth;i<=n&&!yes;++i)
{
if(row_ok(i))
{
dfs(i+1);//注意这里不是dfs(step+1),选的行号是递增的
for(int j=1;j<=m;++j)//还原
if(mp[i][j])
vis[j]=0;
}
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
scanf("%d",&mp[i][j]);
memset(vis,0,sizeof(vis));
yes=0;
dfs(1);
if(yes)
printf("Yes, I found it\n");
if(!yes)
printf("It is impossible\n");
}
return 0;
}

[ACM] POJ 3740 Easy Finding (DFS)的更多相关文章

  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(Dancing Links)

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

  3. POJ 3009-Curling 2.0(DFS)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 5125 Desc ...

  4. 题解报告:poj 1321 棋盘问题(dfs)

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  5. POJ 2251 Dungeon Master(dfs)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

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

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

  7. POJ 2386——Lake Counting(DFS)

    链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...

  8. POJ 1321 棋盘问题(dfs)

    传送门 棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38297   Accepted: 18761 Descri ...

  9. [ACM] poj 1088 滑雪 (内存搜索DFS)

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 73409   Accepted: 27141 Description ...

随机推荐

  1. Spark修炼之道——Spark学习路线、课程大纲

    课程内容 Spark修炼之道(基础篇)--Linux基础(15讲).Akka分布式编程(8讲) Spark修炼之道(进阶篇)--Spark入门到精通(30讲) Spark修炼之道(实战篇)--Spar ...

  2. .Net Framework 之 托管模块与程序集的关系

    一.基本概念: --托管模块:一个标准的32的可移植执行体(PE32)文件或一个标准的64位可移植执行体(PE32+)文件.由用支持CLR的任何一种语言创建的源代码文件,再经过相应的编译器检查语法和分 ...

  3. iOS exit(),abort(),assert()函数区别

    iOS exit(),abort(),assert()函数区别 exit() 退出程序 abort() 停止程序, assert()检查里面的参数如果为nil抛出异常:

  4. CodeForces 390E Inna and Large Sweet Matrix(树状数组改段求段)

    树状数组仅仅能实现线段树区间改动和区间查询的功能,能够取代不须要lazy tag的线段树.且代码量和常数较小 首先定义一个数组 int c[N]; 并清空 memset(c, 0, sizeof c) ...

  5. Spring Bean的作用域类型

    Bean的作用域类型 singleton :在Spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在; prototype :每次从容器中调用Bean时,都返回一个新的实例,即每 ...

  6. struts 页面调用Action的指定方法并传递参数

    如果为action配置了类,那么默认就会执行Action类的excute方法,Action类的写法三种: ① public class Action1 { public String execute( ...

  7. 关于DevOps你必须知道的11件事

    转自:http://www.infoq.com/cn/articles/11devops 关于作者 Gene Kim在多个角色上屡获殊荣:CTO.研究者和作家.他曾是Tripwire的创始人并担任了1 ...

  8. 最接近WeChat的全屏自定义相机(Custom Camera)

    代码地址如下:http://www.demodashi.com/demo/13271.html 一.需求 最接近WeChat的全屏自定义相机(Custom Camera),拍照和预览都是全屏尺寸.使用 ...

  9. 【ajax+php】动态展示4级单位(省、市、县、镇)

    1.本篇教程以ajax+php动态展示[省.市.县.镇]四级地区单位 2.效果图:    3.不废话,贴代码! HTML: <div class="form-group"&g ...

  10. sql server删除数据时如何进行级联删除

    可以在创建外键约束时直接设置级联删除