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

最新文章

  1. [LeetCode] Valid Parentheses 验证括号
  2. cookie相关
  3. Vue in 2016
  4. 洛谷 P1330 封锁阳光大学 Label:染色问题
  5. c#开发工具软件集合
  6. Searching External Data in SharePoint 2010 Using Business Connectivity Services
  7. jQuery页面加载后执行的事件(3种方式)
  8. 避免多层回调,Node.js异步库Async使用(parallel)
  9. block 解析 - 内存
  10. MongoDB--GridFS 文件存储系统
  11. java笔记3(动手动脑)
  12. Linux时间子系统之七:定时器的应用--msleep(),hrtimer_nanosleep()
  13. 任务调度--spring下的任务调度quartz
  14. 深入理解Java 栈数据结构
  15. postman进行https接口测试所遇到的ssl证书问题,参考别人方法
  16. nginx配置打印请求响应内容
  17. 【刷题】BZOJ 2194 快速傅立叶之二
  18. 屏蔽alert弹框下面一层的操作
  19. IOS下HTML5获取焦点 弹键盘
  20. Xshell连接不上虚拟机Linux系统

热门文章

  1. 12V继电器开关控制
  2. 比較具体的handle机制
  3. Installing the .NET Framework 4.5, 4.5.1
  4. Call to undefined function curl_init()解决方法
  5. java web 中的转发和重定向
  6. Android 实现 IOS相机滑动控件
  7. 关于AfterLogic WebMail 的.net版无法上传控件的解决办法
  8. iOS8 iPad Warning: Attempt to present &lt;UIImagePickerController:xxxx &gt; on xxxx which is already presenting (null)
  9. .NET 设计模式之简单工厂模式(二)
  10. Visual C++ 编程实现Soft AP (HostedNetwork / 承载网络) 功能