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. Linux命令:iostat命令

    Linux系统中的 iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况.同 ...

  2. jsp页面 将数据以Json 格式保存到数据库

    1:jsp页面 <div class="control-group form-group all_activity"> <c:choose> <c:w ...

  3. get方法和load方法的区别

    get方法的特点    get方法采用的是立即检索策略(查询):执行到这行的时候,马上发送SQL查询    get方法查询后返回的是真实对象的本身   load方法的特点    load方法采用的是延 ...

  4. Django 学习视图之FBV与CBV

    一. CBV与FBV CBV:Class Based View FBV:Function Based View 我们之前写过的都是基于函数的view,就叫FBV.还可以把view写成基于类的,那就是C ...

  5. Django学习 之 HTTP与WEB为Django做准备

    一.HTTP 1.HTTP 简介 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准. ...

  6. Tomcat的部署、虚拟主机及优化

    Tomcat的部署.虚拟主机及优化 文章目录 Tomcat的部署.虚拟主机及优化 前言 1.Tomcat的名称由来 2.Apache Tomcat 5-7版本差异 2.1Apache Tomcat5. ...

  7. Fescar分布式事务实现原理解析探秘

    前言 fescar发布已有时日,分布式事务一直是业界备受关注的领域,fescar发布一个月左右便受到了近5000个star足以说明其热度.当然,在fescar出来之前,已经有比较成熟的分布式事务的解决 ...

  8. ping命令工具:同时ping多个IP

    检测多个ip在同一时间点的响应状态,通过对比来判断哪个ip异常. 下载地址:https://share.weiyun.com/5XCkypG

  9. Java 理论与实践: 哈希

    有效和正确定义hashCode()和equals() 每个Java对象都有 hashCode() 和 equals() 方法.许多类 Override 这些方法的缺省实施,以在对象实例之间提供更深层次 ...

  10. 进程管理与SELinux

      进程(process):   将程序与进程的总结:  程序 (program):通常为 binary program ,放置在储存媒体中 (如硬盘.光盘.软盘.磁带等), 为实体文 件的型态存在 ...