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.
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.
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.
4 5 10101 01000 00111 10000
YES
4 5 10100 01000 00110 00101
NO
题目大意:
给两个 n,m;
然后输入 n*m 表示第i个开关 可以打开 第j盏灯,而且假如灯已经亮了的话,再按是不会熄灭的。
题目问你,存不存在一个开关,忽略这个开关,按下所有其他开关,其他灯要全亮,存在就输出“YES”
否则就是“NO”
解题思路:
先记录一下,有多少开关可以使第j盏灯亮起来
然后暴力一下。忽略第一个开关,对应的灯的开关数 -1 ,然后观察相对应的灯的开关数减掉后,是否还是 > 0的,因为只要>0,就代表还有别的开关可以控制对应的灯,然后每次去找就可以了,符合条件就输出“YES”;
#include<bits/stdc++.h>
using namespace std;
string a[2005];
int num[2100]; //控制灯的数量
int main()
{
int n,m;
cin >> n >> m;
for(int i=0;i<n;i++)
{
cin >> a[i];
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]=='1') num[j]++;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]=='1') num[j]--;
}
int flag=1;
for(int j=0;j<m;j++)
{
if(num[j]==0)
{
flag=0;
break;
}
}
if(flag==1)
{
cout << "YES" << endl;
return 0;
}
for(int j=0;j<m;j++) //注意这里要加回去!
{
if(a[i][j]=='1') num[j]++;
}
}
cout << "NO" << endl;
}