POJ3740--Easy Finding

Description

Given a   M× N  matrix   A.   A ij  ∈ {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
//此题尼玛就一dfs......补选赛的时候我既然做不出来。我真是弱爆了。。。弱爆了弱爆了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
bool vis[28][320];
bool flag=false;
bool val[28];
int m,n;
void dfs(int c)
{
	int fuck=0;
	for(int i=1;i<=m;i++)
	{
		//先判断已选择的行中,这一列有多少个1了。如果没有1.
		//那就找一个1并且标记。如果找不到。那就false
		//如果有多个1,那么说明这样做是不行的。得回溯
		if(val[i]&&vis[i][c]) fuck++;
	}
	if(fuck>1)return;
	if(fuck==1)
	{
		if(c<n)dfs(c+1);
		else flag=true;
	}
	if(fuck==0)
	{
		for(int i=1;i<=m;i++)
		{
			if(vis[i][c]&&!val[i])
			{
				bool hehe=true;
				for(int j=1;j<c;j++)
				{
					if(vis[i][j])
					{
						hehe=false;
						break;
					}
				}
				if(hehe)
				{
					val[i]=1;
					if(c<n)dfs(c+1);
					else flag=true;
					val[i]=0;
				}
			}
		}
	}	
}
int main()
{
	while(scanf("%d%d",&m,&n)==2)
	{
		memset(vis,0,sizeof(vis));
		memset(val,0,sizeof(val));
		int a;
		flag=false;
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=n;j++)
			{
				scanf("%d",&a);
				vis[i][j]=a==1;
			}
		}
		dfs(1);
		if(flag)cout<<"Yes, I found it"<<endl;
		else cout<<"It is impossible"<<endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值