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. .h头文件 .lib库文件 .dll动态库文件之间的关系

    .h头文件是编译时必须的,lib是链接时需要的,dll是运行时需要的. 附加依赖项的是.lib不是.dll,若生成了DLL,则肯定也生成 LIB文件.如果要完成源代码的编译和链接,有头文件和lib就够 ...

  2. MFC程序实现给对话框加入�背景图片

    1.插入一个Bitmap的资源图片,如果资源名称为:IDC_BITMAP1 2.在CXXXDialog::OnPaint()中实现: void CMyDialogDlg::OnPaint() { if ...

  3. xcode6下使用autolayout+sizeclass实践

    历史车轮滚滚向前,将autolayout配合sizeclass做布局的方式推上了主流,虽然有点晚,但最终还是进行了一次完整的实践,特此记录一下: 因为网上已经有很多博客介绍了autolayout配合s ...

  4. 详解Android动画之Tween Animation

    前面讲了动画中的Frame动画,今天就来详细讲解一下Tween动画的使用. 同样,在开始实例演示之前,先引用官方文档中的一段话: Tween动画是操作某个控件让其展现出旋转.渐变.移动.缩放的这么一种 ...

  5. Windows 下启动Apache服务(转)

    Win下apache出现问题:“No services installed”安装完apache之后(不是按照默认路径安装的,我的是 D:\ )右下方那个小羽毛图标是没有启动的,左键不好使,而且提示“N ...

  6. hadoop错误Operation category READ is not supported in state standby

    报如下错误 解决方法: 方法一:(结果不起作用) 通过Shell命令方式,hadoop/bin/hdfs haadmin -failover --forceactive hadoop2 hadoop1 ...

  7. ios中创建可以拖动的view原理和实现详解

    有时候我们会需要在界面上拖动view;uiview是继承于uiresponder的,所以可以响应触摸相关的事件. 重点是以下一组方法: - (void)touchesBegan:(NSSet *)to ...

  8. JavaScript 是世界上最好的语言?

    2016年1月中旬,Stack Overflow发起本年度的开发者调查,调查结果于近日公布.本文盘点 JS 开发者应该会关心的部分数据. Stack Overflow 技术排行榜: 在2015年6月, ...

  9. JS2 for应用

    for应用  再谈js获取元素一二: var oUl=document.getElementById('list');      //静态方法 var oUl=document.getElements ...

  10. xml--小结②XML的基本语法

    二.XML的基本语法1.文档声明:作用:用于标识该文档是一个XML文档.注意事项:声明必须出现在文档的第一行(之前连空行都不能有,也不能有任何的注释) 最简单的XML声明:<?xml versi ...