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
",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
", ans);
}
return 0;
}