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;
    }
    
  • 相关阅读:
    一站式示例代码库登陆微软中国首页
    一站式示例代码库 中文版 2010年10月更新
    微软一站式示例代码库20101010 新增代码示例简介
    一站式示例代码库 中文版 2010年9月更新
    微软全新示例代码请求服务正式上线
    Merge Sort 归并排序
    递归的Fibonacci在数羊
    VS2010 常用快捷键总结
    【总结——HTTP协议】
    在项目中使用log4net记录日志
  • 原文地址:https://www.cnblogs.com/mmlz/p/5960282.html
Copyright © 2011-2022 走看看