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;
    }
    
  • 相关阅读:
    第十天冲刺个人博客
    第九天冲刺个人博客
    软件工程学习进度表(十六周)
    部署一个件简简单单的项目竟然会遇到这么多事?
    Apache和Tomcat公用80端口
    windows下nginx中配置端口转发 ----本文来自转载
    linux中nginx中配置端口转发
    字体图标:Font Awesome
    最新主流 Markdown 编辑器推荐
    博客园如何转载别人的文章(来自转载)
  • 原文地址:https://www.cnblogs.com/mmlz/p/5960282.html
Copyright © 2011-2022 走看看