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;
}

CodeforcesGym101116 B Bulbs的更多相关文章

  1. Codeforces Round #338 (Div. 2) A. Bulbs 水题

    A. Bulbs 题目连接: http://www.codeforces.com/contest/615/problem/A Description Vasya wants to turn on Ch ...

  2. HDU 5601 N*M bulbs 找规律

    N*M bulbs 题目连接: http://codeforces.com/contest/510/problem/C Description NM个灯泡排成一片,也就是排成一个NM的矩形,有些开着, ...

  3. BestCoder Round #67 (div.2) N bulbs(hdu 5600)

    N bulbs Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  4. N bulbs(规律)

    N bulbs  Accepts: 408  Submissions: 1224  Time Limit: 10000/5000 MS (Java/Others)  Memory Limit: 655 ...

  5. Bulbs【暴力?】

    问题 B: Bulbs 时间限制: 1 Sec  内存限制: 128 MB 提交: 216  解决: 118 [提交] [状态] [命题人:admin] 题目描述 Greg has an m × n ...

  6. zoj 2976 Light Bulbs(暴力枚举)

    Light Bulbs Time Limit: 2 Seconds      Memory Limit: 65536 KB Wildleopard had fallen in love with hi ...

  7. 哈理工2015 暑假训练赛 zoj 2976 Light Bulbs

    MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu SubmitStatusid=14946">Practice ...

  8. upc组队赛5 Bulbs

    Bulbs 题目描述 Greg has an m × n grid of Sweet Lightbulbs of Pure Coolness he would like to turn on. Ini ...

  9. 2019 ACM-ICPC 上海网络赛 B. Light bulbs (差分)

    题目链接:Light bulbs 比赛链接:The Preliminary Contest for ICPC Asia Shanghai 2019 题意 给定 \(N\) 个灯泡 (编号从 \(0\) ...

随机推荐

  1. POJ 1006 Biorhythms(中国剩余定理)

    题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...

  2. PHP文件头部(header)解释

    1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Charset:   浏览器申 ...

  3. 打开PPT 提示安装,非要取消才能显示PPT

    自从安装VS2008后,一打开ppt文件,就开始了配置VS2008的过程. 配置完了,下回也还提示安装. 解决方案: PowerPoint选项->加载项->Chinese Translat ...

  4. mina编解码(摘录)

    一.Mina对编解码的支持 我们知道网络通讯过程实际是对二进制数据进行处理的过程,二进制数据是计算机认识的数据.对于接收到的二进制数据我们需要将其转换成我们所熟悉的数据格式,此过程称为解码(decod ...

  5. PHP解决多进程同时读写一个…

    原文地址:PHP解决多进程同时读写一个文件的问题作者:陌上花开 首先PHP是支持进程的而不支持多线程(这个先搞清楚了),如果是对于文件操作,其实你只需要给文件加锁就能解决,不需要其它操作,PHP的fl ...

  6. php之常量小见

    php设置常量有二种方法,一为define(),二为使用关键字const. define()函数带有三个形参,一为常量名,通常以大写字母命名,二为值,三为是否对大小写敏感,其值为可选,默认为false ...

  7. .NET平台下几种SOCKET模型的简要性能供参考

    转载自:http://www.cnblogs.com/asilas/archive/2006/01/05/311309.html .NET平台下几种SOCKET模型的简要性能供参考 这个内容在cnbl ...

  8. ideadpad-安装win7

    今天帮一个同学的朋友安装win7, 竟然安不上,很久没有出现安不上系统了, 后来发现他的笔记本是新的类型,预先注意事项为: 1,启动的时候,使用 Fn+F2进入 BIOS. 2,将Boot中的Boot ...

  9. MyEclipse激活失败,解决办法

    文章参考:http://www.cnblogs.com/dingyuanxin/p/4046356.html 失败可能是:systemid和exe破解出来的那个对应不上: 1.启动MyEclipse, ...

  10. eclipse总是自动跳到ThreadPoolExecutor.java

    解决方法:在eclipse中选择Window->Preference->Java->Debug, 将“Suspend execution on uncaught exceptions ...