題目描述
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("%d\n",ans);}return 0;
}
?