POJ 3740 Easy Finding(dfs回溯)

B - Easy Finding

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice POJ
3740

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 Nintegers 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

这是一道子集枚举的回溯题,针对当前行选不选进行回溯,并且进行剪枝。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 1005
int n,m;
int a[20][305];
int vis[305];
bool judge(){
   for(int i=0;i<n;i++)
    if(vis[i]==0)return false;
   return true;
}
bool judges(){
   for(int i=0;i<n;i++)
    if(vis[i]>1)return false;
   return true;
}
bool dfs(int cur)
{
    if(judge())return true;
    if(cur>=m)return judge();
    for(int i=0;i<n;i++){
        if(a[cur][i]==1)vis[i]++;
    }
    if(judges()){
        if(dfs(cur+1))return true;
    }
    for(int i=0;i<n;i++){
        if(a[cur][i]==1)vis[i]--;
    }
    if(dfs(cur+1))return true;
    return false;
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&m,&n)!=EOF){
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            scanf("%d",&a[i][j]);
        memset(vis,0,sizeof vis);
        if(dfs(0))puts("Yes, I found it");
        else puts("It is impossible");

    }

}
时间: 2024-10-12 20:49:22

POJ 3740 Easy Finding(dfs回溯)的相关文章

[ACM] POJ 3740 Easy Finding (DFS)

Easy Finding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16482   Accepted: 4476 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.

poj 3740 -- Easy Finding (dfs)

题目大意:给出一个m行n列的数组,元素只有0和1, 问:能不能找出几行,使得每一列都有且仅有一个1. 分析:直接深搜即可 #include<iostream> #include<cstdio> using namespace std; int vis[311];//记录该列有1没 int n, m; int a[20][311]; bool flag; bool fuhe(int i){ for (int j = 1; j <= n; j++) if (a[i][j] &am

poj 3740 Easy Finding(Dancing Links)

Easy Finding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15668   Accepted: 4163 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.

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

Easy Finding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16178   Accepted: 4343 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.

[ACM] POJ 3740 Easy Finding (DFS)

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 a

POJ 3740 Easy Finding

题目链接:http://poj.org/problem?id=3740 dancing links 入门题 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <string> 5 #include <iomanip> 6 using namespace std; 7 int M, N; 8 #define maxn 16*300+5 9 int R[

POJ 3740 Easy Finding DLX

题意:给你一个0,1矩阵 ,求精确覆盖 解题思路: DLX 解题代码: 1 // File Name: poj3740.cpp 2 // Author: darkdream 3 // Created Time: 2014年10月04日 星期六 20时06分31秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #i

POJ 1321 棋盘问题(dfs回溯)

A - 棋盘问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1321 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每

poj 1154 letters (dfs回溯)

http://poj.org/problem?id=1154 #include<iostream> using namespace std; int bb[26]={0},s,r,sum=1,s1=1; char aa[25][25]; int dir[4][2]={-1,0,1,0,0,-1,0,1}; void dfs(int a,int b) { int a1,b1; if(s1>sum) sum=s1; //更新最大数值 for(int i=0;i<4;i++) { a1=