zoukankan      html  css  js  c++  java
  • CodeforcesGym101116 B Bulbs

    Description

    Greg has an (m imes n) grid of Sweet Lightbulbs of Pure Coolness he would like to turn on. Initially, some of the bulbs are on and some are off. Greg can toggle some bulbs by shooting his laser at them. When he shoots his laser at a bulb, it toggles that bulb between on and off. But, it also toggles every bulb directly below it,and every bulb directly to the left of it. What is the smallest number of times that Greg needs to shoot his laser to turn all the bulbs on?

    Input

    The first line of input contains a single integer (T (1 le T le 10)), the number of test cases. Each test case starts with a line containing two space-separated integers (m) and (n) ((1 le m, n le 400)). The next (m) lines each consist of a string of length (n) of (1)s and (0)s. A (1) indicates a bulb which is on, and a (0) represents a bulb which is off.

    Output

    For each test case, output a single line containing the minimum number of times Greg has to shoot his laser to turn on all the bulbs.

    Sample Input

    2
    3 4
    0000
    1110
    1110
    2 2
    10
    00

    Sample Output

    1
    2

    对于这题,有个朴素做法——高斯消元解异或方程组(每个灯泡为一个方程,每个能够影响此灯泡的为次方程未知元)。但这样明显过不去,经过仔细观察,可以发现这个方程可以(O(N^{2}))推出解,统计解为(1)的个数即为答案。

    #include<cstring>
    #include<bitset>
    #include<cstdio>
    #include<cstdlib>
    using namespace std;
    
    #define maxn (410)
    int ans,T,M,N,bulb[maxn][maxn],up[maxn];
    
    inline int id(int x,int y) { return (x-1)*N+y-1; }
    
    int main()
    {
    	freopen("B.in","r",stdin);
    	freopen("B.out","w",stdout);
    	scanf("%d",&T);
    	while (T--)
    	{
    		scanf("%d %d",&M,&N); memset(up,0,sizeof(up)); ans = 0;
    		for (int i = 1,now = 0;i <= M;++i)
    			for (int j = 1;j <= N;++j,++now)
    				scanf("%1d",bulb[i]+j);
    		for (int i = 1;i <= M;++i)
    			for (int j = N,ri = 0;j;--j)
    			{
    				int res = (bulb[i][j]^1^ri^up[j]);
    				ri ^= res; up[j] ^= res; ans += res;
    			}
    		printf("%d
    ",ans);
    	}
    	fclose(stdin); fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    Activemq+Zookeeper集群
    Centos7 安装 ActiveMQ 5.15.1
    189. Rotate Array
    188. Best Time to Buy and Sell Stock IV
    187. Repeated DNA Sequences
    186.Reverse Words in a String II
    179. Largest Number
    174. Dungeon Game
    173. Binary Search Tree Iterator
    172. Factorial Trailing Zeroes
  • 原文地址:https://www.cnblogs.com/mmlz/p/5960282.html
Copyright © 2011-2022 走看看