Problem B: Bulbs

Greg has an m×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 ≤ T ≤ 10), the number of test cases. Each test case starts with a line containing two space-separated integers m and n (1 ≤ m,n ≤ 400). The next m lines each consist of a string of length n of 1s and 0s. 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
Explanation
In the first test case, shooting a laser at the top right bulb turns on all the bulbs which are off, and does not toggle any bulbs which are on.
In the second test case, shooting the top left and top right bulbs will do the job.

题意:把所有0,变成1最少需要几次;规则:如果把0变成1同时把左边的数和下边的数同时改变,

题解:从左上角开始处理,for循环遍历即可

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
int n,m,ans=;
int a[],b[];
string s[];
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
ans=;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i=;i<n;i++)
cin>>s[i];
for(int i=;i<n;i++)
{
for(int j=m-;j>=;j--)
{
int x=s[i][j]-''+a[i]+b[j];
if(x%==)
{
ans++;
a[i]++;
b[j]++;
}
}
}
cout<<ans<<endl;
}
}

Problem 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. zoj 2976 Light Bulbs(暴力枚举)

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

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

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

  7. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  8. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  9. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

随机推荐

  1. Oracle个人自学笔记

    SET LINESIZE 300;//设置每一行的长度 SET PAGESIZE 100;//设置每一列的长度 CONN 用户名/密码 [AS SYSDBA],如果是sys用户一定要加上SYSDBA ...

  2. java.lang.ClassCastException: android.app.Application cannot be cast to

    出这个异常的原因是在项目中添加了新lication类(public class Application extends lication)之后,没有在AndroidManifest.xml中添加该类的 ...

  3. Linux centosVMware 负载均衡集群介绍、LVS介绍、LVS调度算法、LVS NAT模式搭建

    一.负载均衡集群介绍 主流开源软件LVS.keepalived.haproxy.nginx等 其中LVS属于4层(网络OSI 7层模型),nginx属于7层,haproxy既可以认为是4层,也可以当做 ...

  4. git - 节点树理解

    1.如下图 最左边黑色线是当前分支节点 每个点代表一次提交 2.当执行merge prd的时候 会把其他分支的节点都merge到你的分支同时产生右边的每条节点线. 3.每个版本都会与某条版本线上的一个 ...

  5. 「luogu2633」Count on a tree

    「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...

  6. Python 爬取 北京市政府首都之窗信件列表-[后续补充]

    日期:2020.01.23 博客期:131 星期四 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] //博客总体说明 1.准备工作 2.爬取工作(本期博客) 3.数据处理 ...

  7. stringstream常见用法介绍

    1 概述 <sstream> 定义了三个类:istringstream.ostringstream 和 stringstream,分别用来进行流的输入.输出和输入输出操作.本文以 stri ...

  8. 指定GPU运行python程序

    一.命令行运行python程序时 1.首先查看哪些GPU空闲,nvidia-smi显示当前GPU使用情况. nvidia-smi 2.然后指定空闲的GPU运行python程序. CUDA_VISIBL ...

  9. PIL pip error

    结果显示: 提示——Could not find a version that satisfies the requirement PIL (from versions: )No matching d ...

  10. mycat 主从复制,读写分离,

    1.mycat 主从复制 https://blog.csdn.net/qq_35992900/article/details/80599976 2.mycat 读写分离 https://blog.cs ...