题目描述

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.

 

题目大意:

先输入一个整数t,代表共有t组测试,对于每一组测试,先输入两个整数n,m,然后输入n行m列的0,1表,问共需要几步操作能够将这个二维数组的所有值均变为1,对于每一个操作,假如对a[i][j]进行一次操作,则将a[i][j],从a[1][j]到a[i][j],以及从a[i][j]到a[n][j]的所有值取反,即0变1,1变0,问最终需要至少几步操作。

解题思路:

因为每次操作仅会改变其左边,下面的值,所以可以从数组的右上角开始遍历,对于每一个0进行一次操作,最后输出操作次数即可。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int hang[500];
int lie[500];
int arr[500][500];
int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int t;scanf("%d",&t);while(t--) {int n,m;scanf("%d %d",&n,&m);rep(i,1,n) {hang[i]=0;rep(j,1,m) {lie[j]=0;scanf("%1d",&arr[i][j]);}}int ans=0;rep(i,1,n) {lep(j,m,1) {if((arr[i][j]==0&&(hang[i]+lie[j])%2==0)||(arr[i][j]==1&&((hang[i]+lie[j])%2==1))) {ans++;hang[i]++;lie[j]++;}}}printf("%dn",ans);}return 0;
}