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. android 双卡手机发短信/判断手机是否为双卡

    通过反射发现sendMultipartTextMessage多了一个phoneType的参数,这个参数就是这只用GMS还是CDMA卡发送的. phoneType获取方法,iTelephony.getA ...

  2. 追踪CPU跑满 堆栈调试

    http://blog.donghao.org/2014/04/24/%E8%BF%BD%E8%B8%AAcpu%E8%B7%91%E6%BB%A1/

  3. PCAP 抓包

    PCAP是一个数据包抓取库, 很多软件都是用它来作为数据包抓取工具的. WireShark也是用PCAP库来抓取数据包的.PCAP抓取出来的数据包并不是原始的网络字节流,而是对其进行从新组装,形成一种 ...

  4. GUI编程笔记(java)05:GUI事件监听机制原理和举例说明

    1.事件监听机制:       A:事件源          事件发生的地方       B:事件             就是要发生的事情       C:事件处理       就是针对发生的事情做 ...

  5. javascript闭包分析

    闭包是什么?闭包是Closure,简而言之,闭包就是: 闭包就是函数的局部变量集合,只是这些局部变量在函数返回后会继续存在. 闭包就是就是函数的“堆栈”在函数返回后并不释放,我们也可以理解为这些函数堆 ...

  6. 【原创教程】虎咽CSS

      上节课我们讲了HTML基础,回顾的事我不干了,因为你可以回头看很多遍,这节课我们来学习下CSS这门艺术的基础知识,一直以来我们都是CSS,CSS那么CSS到底是什么呢,惯例,我又来一层一层把CSS ...

  7. 请问JAVA三层架构,持久层,业务层,表现层,都该怎么理解?和MVC三层模型有什么区别

    持久层用来固化数据,如常说的DAO层,操作数据库将数据入库业务层用来实现整体的业务逻辑 如 前台获得了数据,逻辑层去解析这些数据,效验这些数据等操作表现层很好解释  你现在看到的网页 一些界面 都属于 ...

  8. Java写一个简单学生管理系统

    其实作为一名Java的程序猿,无论你是初学也好,大神也罢,学生管理系统一直都是一个非常好的例子,初学者主要是用数组.List等等来写出一个简易的学生管理系统,二.牛逼一点的大神则用数据库+swing来 ...

  9. css-a:visited

    如下代码: <a href="#">链接地址</a> 如果属性 href的设置为'#',则鼠标滑过(即使没有点击',也算成'visited'.而对于 hre ...

  10. Oracle 11g 新特性(一)-- 虚拟列

    数据库版本: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Oracle11g 增加了虚拟列的新特性, 具体说明如 ...