(水 dfs) poj 3740

Easy Finding

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16854   Accepted: 4567

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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int n,m,mp[305][305],vis[305];
bool check()
{
    for(int i=0;i<m;i++)
        if(vis[i]!=1)
            return false;
    return true;
}
bool ok()
{
    for(int i=0;i<m;i++)
    {
        if(vis[i]>1)
            return false;
    }
    return true;
}
bool dfs(int x)
{
    if(check())
        return true;
    for(int i=x;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            vis[j]+=mp[i][j];
        }
        if(ok())
        {
            if(dfs(i+1))
                return true;
        }
        for(int j=0;j<m;j++)
        {
            vis[j]-=mp[i][j];
        }
    }
    return false;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                scanf("%d",&mp[i][j]);
        }
        if(dfs(0))
        {
            printf("Yes, I found it\n");
        }
        else
            printf("It is impossible\n");
    }
    return 0;
}

  

时间: 2024-12-13 15:03:02

(水 dfs) poj 3740的相关文章

[水+dfs] poj 2034 Anti-prime Sequences

题意: 给n,m,k. 排列n~m之间的所有数. 保证相邻的2~k位之和均不为素数. 思路: 直接DFS. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"queue" #include"algorithm" #include"iostream" #in

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 A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn

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.

dfs/poj 2488 A Knight&#39;s Journey

1 #include<cstdio> 2 using namespace std; 3 const int b[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; 4 int a[30][30],p,q; 5 struct 6 { 7 int x,y; 8 }step[910]; 9 10 bool dfs(int x,int y,int now) 11 { 12 if (now==p*q) return true;

DFS POJ 3087 Shuffle&#39;m Up

题目传送门 1 /* 2 题意:两块扑克牌按照顺序叠起来后,把下半部分给第一块,上半部给第二块,一直持续下去,直到叠成指定的样子 3 DFS:直接模拟搜索,用map记录该字符串是否被搜过.读懂题目是关键. 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-3 13:57:55 8 File Name :POJ_3087.cpp 9 *****

搜索 || DFS || POJ 2488 A Knight&#39;s Journey

给一个矩形棋盘,每次走日字,问能否不重复的走完棋盘的每个点,并将路径按字典序输出 *解法:按字典序输出路径,因此方向向量的数组按字典序写顺序,dfs+回溯,注意flag退出递归的判断,并且用pre记录路径 #include <iostream> #include <cstdio> #include <cstring> using namespace std; char a[30][30]; int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2};

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

[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

[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.