B. Switches and Lamps

本文探讨了一个经典的开关灯问题,通过暴力枚举的方法来判断是否可以忽略某个开关的同时保证所有灯处于开启状态。提供了完整的实现代码,并详细解释了背后的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix aconsisting of n rows and m columns where ai, j = 1 if the i-th switch turns on the j-th lamp and ai, j = 0 if the i-th switch is not connected to the j-th lamp.

Initially all m lamps are turned off.

Switches change state only from "off" to "on". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards.

It is guaranteed that if you push all n switches then all m lamps will be turned on.

Your think that you have too many switches and you would like to ignore one of them.

Your task is to say if there exists such a switch that if you will ignore (not use) it but press all the other n - 1 switches then all the m lamps will be turned on.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 2000) — the number of the switches and the number of the lamps.

The following n lines contain m characters each. The character ai, j is equal to '1' if the i-th switch turns on the j-th lamp and '0' otherwise.

It is guaranteed that if you press all n switches all m lamps will be turned on.

Output

Print "YES" if there is a switch that if you will ignore it and press all the other n - 1 switches then all m lamps will be turned on. Print "NO" if there is no such switch.

Examples

input

Copy

 

4 5 10101 01000 00111 10000

output

Copy

 

YES

input

Copy

 

4 5 10100 01000 00110 00101

output

Copy

 

NO

题目大意:给出n个串,m为每个串的长度,每个串中的1代表该串的操作能使该位置变为1(开灯),要求判断是否存在去除一个串的操作使所有灯保持全开的状态。

 

 

思路:暴力,枚举,用一个数组存灯的累加状态,枚举每操作一次串所得的状态。

代码如下:

#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<queue>
#include<math.h>
#include<map>
#include<set>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int p[2005]={0};
int main()
{
	int n,m,i,j,k;
	scanf("%d%d",&n,&m);
	string s[n];
	for(i=0;i<n;i++)
	{
		cin>>s[i];
		for(j=0;j<s[i].size();j++)
		{
			p[j]+=s[i][j]-'0';//状态累加
		}
	}
	int fag=0;
	for(i=0;i<n;i++)
	{
		int fag1=0;
		for(j=0;j<m;j++)
		{
			if(s[i][j]=='1') p[j]--;
			if(p[j]==0) fag1=1;
		}
		if(fag1==0)
		{
			fag=1;
		}
		for(j=0;j<m;j++)
		{
			if(s[i][j]=='1') p[j]++;
		}
	}
	if(fag==1)
	printf("YES");
	else
	printf("NO");
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值