CodeforcesGym101116 B Bulbs

Description

Greg has an \(m \times 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\n",ans);
    }
    fclose(stdin); fclose(stdout);
    return 0;
}

转载于:https://www.cnblogs.com/mmlz/p/5960282.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值