码迷,mamicode.com
首页 > 其他好文 > 详细

(水 dfs) poj 3740

时间:2015-04-15 21:21:36      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

  

(水 dfs) poj 3740

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4430041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!