http://poj.org/problem?id=3740

这是一道搜索+回溯的题目,也是我第一次接触到回溯。

题意就是找一些行,这些行可以使每一列都只存在一个1。

深搜加回溯:

memory:118K c++ runtime:674ms。
#include <stdio.h>
#include <string.h>
#include <iostream> using namespace std; int a[][],n,m;
bool used[],fin; bool judge() //判断是否已经寻找到那些行加起来可以使所有的列只存在一个1。
{
for(int i=;i<=m;i++)
if(!used[i]) return false;
return true;
}
bool check(int row) //判断当前行是否有与之前行在某一列有冲突(都有1)
{
for(int i=;i<=m;i++)
if(used[i]&&a[row][i]) return false; // 如果都有1的话,回溯。
for(int i=;i<=m;i++)
if(a[row][i]) used[i]=true; //如果不冲突的话,则把这一行用上,并把其的所有的1所在的行都标记上。
return true;
} void dfs(int s)
{
if(fin||s>n+) return; //判断退出的标志,即输出的结果,或者行数已经超过了n+1。
if(judge()) {
printf("Yes, I found it\n");
fin=true;
return;
}
for(int i=s;i<=n&&!fin;i++)
{
if(check(i)){
dfs(i+);    
for(int j=;j<=m;j++) //这就是回溯,因为如果I+1与之前的有冲突的话,if(check(i+1))则为false。所以执行的就应该是这一行。
if(a[i][j]) used[j]=false;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,,sizeof(a));
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&a[i][j]);
fin=false;
memset(used,false,sizeof(used));
dfs();
if(!fin) printf("It is impossible\n");
}
return ;
}

POJ 3740的更多相关文章

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

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

  2. 【POJ 3740】 Easy Finding

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

  3. Easy Finding POJ - 3740 (DLX)

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

  4. poj 3740 Easy Finding(Dancing Links)

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

  5. [ACM] POJ 3740 Easy Finding (DFS)

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

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

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

  7. POJ 3740 DLX

    题意:给你一个01矩阵,然后求是否存在选择一些行,使得每一列的1的个数都为1. 思路:貌似朴素的DFS也可以,加点剪枝就可以过.这里贴个DLX的模版. 推荐博客:http://www.cppblog. ...

  8. poj 3740 Easy Finding 精确匹配

    题目链接 dlx的第一题, 真是坎坷..... #include <iostream> #include <vector> #include <cstdio> #i ...

  9. POJ 3740 Easy Finding

    #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...

随机推荐

  1. OC-类方法

    类方法 1. 基本概念 直接可以用类名来执行的方法(类本身会在内存中占据存储空间,里面有类\对象方法列表) 2. 类方法和对象方法对比 1)  对象方法 以减号-开头 只能让对象调用,没有对象,这个方 ...

  2. Xunsearch 中文全文搜索

    原文地址:http://www.yiichina.com/code/661 官网地址:http://www.xunsearch.com/ 1.安装 wget http://www.xunsearch. ...

  3. gitingore

    **/.DS_Store node_modules/ logs/*.log views/dir/*.tpl(视图文件后缀)

  4. jQuery EasyUI API 中文文档 - ComboGrid 组合表格

    jQuery EasyUI API 中文文档 - ComboGrid 组合表格,需要的朋友可以参考下. 扩展自 $.fn.combo.defaults 和 $.fn.datagrid.defaults ...

  5. mysql tinyint

    在MySQL的数据类型中,Tinyint的取值范围是:带符号的范围是-128到127.无符号的范围是0到255(见官方<MySQL 5.1参考手册>http://dev.mysql.com ...

  6. Linux鲜为人知的安全漏洞:不要将输出内容管道给你的shell

    将wget或curl输出的内容管道给bash或者sh是一件非常愚蠢的事,例如像下面这样: wget -O - http://example.com/install.sh | sudo sh 命令解释: ...

  7. document.all的详细解释(document.all基本上所有浏览器可用!)

    从何而来从IE4开始IE的object model才增加了document.all对象,MSDN中也对 Object.all 有详细的说明,Object.all是个HTMLCollection,不是数 ...

  8. [译]在AngularJS中何时应该使用Directives,Controllers或者Service

    原文: http://kirkbushell.me/when-to-use-directives-controllers-or-services-in-angular/ Services Servic ...

  9. VTK初学一,a Mesh from vtkImageData—球冠

    #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...

  10. github及其他记录

    http://mvnrepository.com/artifact/org.jdom/jdom/1.1.3 https://github.com/open-power-workgroup/Hospit ...