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?

输入

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.

输出

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.

样例输入

2
3 4
0000
1110
1110
2 2
10
00

样例输出

1
2

提示

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.

题意

题意: 给你一个灯泡组成的图 1代表开 0代表关 每次操作可以切换一个灯的状态,会导致同行的左边所有灯泡和同列的下边所有灯泡状态都会改变 问把所有灯都打开(全1)最少需要几次操作

题解

别问,问就是暴力。。从上到下,从右到左,如果遇到0就改变左边和下边的状态。

代码

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define rep(i,a,n) for(int i=a;i<n;++i)
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define sca(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef pair<int,int> P;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const int MAXN = 105;
const int maxn = 405;
char b[maxn][maxn];
int a[maxn][maxn];
int n, m;
int ans ;
int main() {
int t;
read(t);
while (t--) {
read2(n,m);
for (int i = 0; i < n; i++) {
scanf("%s", b[i]);
for (int j = 0; j < m; j++) {
a[i][j] = b[i][j] - '0';
}
}
ans = 0;
for (int i = 0; i < n; i++) {
for (int j = m - 1; j >= 0; j--) {
if (a[i][j] == 0) { //如果遇到0就改变左边和下边的状态
ans++;
for (int left = 0; left <= j - 1; left++) {
a[i][left] ^= 1;
}
for (int down = i + 1; down < n; down++) {
a[down][j] ^= 1;
}
}
}
}
printf("%d\n", ans);
}
return 0;
}

最新文章

  1. 初识java之String与StringBuffer(上)
  2. js语法重点
  3. [html]经验集
  4. Bzoj3893 [Usaco2014 Dec]Cow Jog
  5. vb 和vb.net的区别
  6. 在oracle中where 子句和having子句中的区别
  7. oracle查询语句【转载】
  8. 防止 NSTimer retain 作为 target 的 self
  9. mousewheel 与 DOMMouseScroll
  10. Mybatis JPA 插件简介(v2.1.0)
  11. Leetcode_28_Implement strStr
  12. GridView 的简单应用
  13. 记一次bond引起的网络故障
  14. mysql开启general_log查看执行sql
  15. (01背包 dp)P1049 装箱问题 洛谷
  16. vue -webpack.dev.config.js模拟后台数据接口
  17. domReady
  18. poj1573模拟
  19. 如何利用IPv6进行远程桌面连接
  20. LRU的理解与Java实现

热门文章

  1. leetcode-解题记录 1108. IP 地址无效化
  2. 2019PhpStrom注册码(破解)+汉化(中文)
  3. JS原型与原型链终极详解 (转载)
  4. golang的数据类型之布尔类型
  5. tp5 之 &quot;No input file specified
  6. scrapy错误-[scrapy.core.scraper] ERROR: Spider error processing
  7. centos7下查看cup核数
  8. P1001 A+B Problem
  9. 使用腾讯地图请求来源未被授权, 此次请求来源域名/ip:servicewechat.com
  10. linux下各文件夹的结构及其用途说明