zoukankan      html  css  js  c++  java
  • Switches and Lamps(思维)

    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
    4 5
    10101
    01000
    00111
    10000
    output
    YES
    input
    4 5
    10100
    01000
    00110
    00101
    output
    NO

     

     

     

    Description

    给你 n 个开关和 m 个灯。 第 i 个开关打开某一些灯。 该信息以矩阵形式(n 行 m 列),如果第 i 个开关能打开第 j 个灯,则 a ij = 1,如果不能打开则 a ij = 0
     
    最初,所有 m 灯都关闭。
     
    开关状态仅能从“关”变为“开”。 这意味着如果两个或更多开关连接到同一个灯,那么在任何一个开关被按下后,灯将会开启,并且即使连接到这个灯的任何其他开关被按下,它也将保持开启。
     
    保证如果你打开所有 n 个开关,那么所有的 m 个灯都将打开。
     
    你的任务就是找出是否存在这样一个开关,如果你忽略(不使用)它,而是按下所有其他的n-1个开关,那么所有的m个灯都将打开。

    Input

    输入的第一行包含两个整数 n 和 m(1 ≤ n, m ≤ 2000) - 开关的数量和灯的数量。
     
    以下n行每行包含m个字符。 如果第i个开关打开第j个灯,则字符 a ij 等于 '1',否则等于 '0'。
     
    保证如果你按下所有 n 个开关,所有 m 个灯都将被打开。

    Output

    如果有一个开关,如果你忽略它并按下所有其他的n - 1开关,所有的m灯都将打开,输出“YES”, 如果没有这样的开关,输出“NO”。

    Sample Input

    Input

    4 5
    10101
    01000
    00111
    10000
    

    Output

    YES

    Input

    4 5
    10100
    01000
    00110
    00101
    

    Output

    NO


    解题思路:有n个开关,m盏灯,也就是邻接矩阵的行所代表的是开关,即为第i个开关控制第j盏灯,先求出每一列之和,即每一盏灯控制它的开关数,
    然后对每一个开关遍历,如果去掉
    开关,能控制这一盏灯的开关数为0,则说明该开关并不是多余的并不能取掉,若是不影响则说明可以取掉
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int n,m,flag,i,j;
        char x[2010];
        int a[20][20];
        int sum[2010];
        memset(a,0,sizeof(a));
        memset(sum,0,sizeof(sum));
        scanf("%d%d",&n,&m);
        getchar();
        for(i=1; i<=n; i++)
        {
            gets(x);
            for(j=1; j<=m; j++)
            {
                a[i][j]=x[j-1]-'0';
                if(a[i][j]==1)
                {
                    sum[j]++;//每一列之和
                }
            }
        }
        flag=0;
        for(i=1; i<=n; i++)//
        {
            for(j=1; j<=m; j++)//
            {
                if(sum[j]-a[i][j]==0)//对于每一列来说去掉这一行会变成0,说明会有灯不受控制
                {
                    break;
                }
            }
            if(j==m+1)//找到一个一个开关不用也不会使灯受影响的
            {
                flag=1;
            }
        }
        if(flag)
        {
            printf("YES
    ");
        }
        else
        {
            printf("NO
    ");
        }
        return 0;
    }


  • 相关阅读:
    【LeetCode】206. Reverse Linked List
    【LeetCode】160. Intersection of Two Linked Lists
    【LeetCode】190. Reverse Bits
    【LeetCode】165. Compare Version Numbers
    继续深入《一张神奇的图》
    Base64编码简介
    证明任意两个正整数相等(伪命题)
    DEADBEEF
    汉诺塔问题
    字符编码(2)-- 程序中的编码
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9229614.html
Copyright © 2011-2022 走看看