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. Solidworks的Toolbox拖出来的零件另存也没用,重新打开之后被自动替换怎么办

    工具-选项-系统选项-异型孔向导,取消勾选"将此文件夹设为Toolbox零部件的默认搜索位置"   这样把Toolbox的零部件另存之后,就可以修改,比如添加草图和特征,然后另存之 ...

  2. 优化SQL Server的内存占用之执行缓存

    在论坛上常见有朋友抱怨,说SQL Server太吃内存了.这里笔者根据经验简单介绍一下内存相关的调优知识   首先说明一下SQL Server内存占用由哪几部分组成.SQL Server占用的内存主要 ...

  3. Drupal与其它开源系统的整合

    网上看到一篇介绍Drupal与phpbb整合的文章.浏览了一下,真心地不错.于是就想将与整合有关的文章做一个汇总,以备不时之需: Drupal7整合PHPBB论坛 Drupal 7 整合 Vanill ...

  4. ORA-01400: 无法将 NULL 插入 ("CHARGE_WQRL"."SF_JMQTFY_T"."BH")

    [ERROR] JmCheckManageDaoImpl:901 - java.sql.SQLException: ORA-01400: 无法将 NULL 插入 ("CHARGE_WQRL& ...

  5. Hacker - 世界上第一个黑客

    http://juliet.iteye.com/blog/176525凯文·米特尼克,1964年生于美国加州的洛杉矶. 13岁时他对电脑着了迷,掌握了丰富的计算机知识和高超的操作技能,但却因为用学校的 ...

  6. 为centos桌面增加在右键中打开终端

    万万没有想到这只是安装一个程序的总是,而不是配置的问题.注意要用root身份才能安装软件 1. yum -y install nautilus-open-terminal 2. reboot

  7. linux一条命令添加一个root级别账户并设置密码

    内网机器提权添加账户,无回显,设置密码就不好弄,下面就是添加一个root级别的账户并设置密码的命令   ? 1 useradd -p `openssl passwd -1 -salt 'lsof' a ...

  8. unity5, UI Button "On Button Down"

    unity5自带的UI Button的Inspector面板中只有On Click事件,如果我们想让一个按钮响应On Button Down事件该怎么办呢?方法是: 点Add Component-&g ...

  9. SecureCRT终端上使用spark-shell时按退格键无反应的解决方法

    问题:用SecureCRT远程连接至Spark集群,启动spark-shell却发现输错命令后却无法用退格键删除. 解决方法: 第一步: 在SecureCRT的菜单栏选择“OPtions(选项)”按钮 ...

  10. First Objective C

    // // main.m // hello-objc // // Created by test5d on 14-9-29. // Copyright (c) 2014年 test5d. All ri ...