N*M bulbs
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 420 Accepted Submission(s): 246
Problem Description
N*M bulbs are in a rectangle, some are on, and some are off.
in order to save electricity, you should turn off all the lights, but you're lazy.
coincidentally,a passing bear children paper(bear children paper means the naughty boy), who want to pass here from the top left light bulb to the bottom right one and leave.
he starts from the top left light and just can get to the adjacent one in one step.
But after all,the bear children paper is just a bear children paper. after leaving a light bulb to the next one, he must touch the switch, which will change the status of the light.
your task is answer whether it's possible or not to finishing turning off all the lights, and make bear children paper also reach the bottom right light bulb and then leave at the same time.
in order to save electricity, you should turn off all the lights, but you're lazy.
coincidentally,a passing bear children paper(bear children paper means the naughty boy), who want to pass here from the top left light bulb to the bottom right one and leave.
he starts from the top left light and just can get to the adjacent one in one step.
But after all,the bear children paper is just a bear children paper. after leaving a light bulb to the next one, he must touch the switch, which will change the status of the light.
your task is answer whether it's possible or not to finishing turning off all the lights, and make bear children paper also reach the bottom right light bulb and then leave at the same time.
Input
The first line of the input file contains an integer T, which indicates the number of test cases.
For each test case, there are n+1 lines.
The first line of each test case contains 2 integers n,m.
In the following n line contains a 01 square, 0 means off and 1 means on.
* T≤10
* N,M≤1000
For each test case, there are n+1 lines.
The first line of each test case contains 2 integers n,m.
In the following n line contains a 01 square, 0 means off and 1 means on.
* T≤10
* N,M≤1000
Output
There should be exactly T lines in the output file.
The i-th line should only contain "YES" or "NO" to answer if it's possible to finish.
The i-th line should only contain "YES" or "NO" to answer if it's possible to finish.
Sample Input
1 1 5 1 0 0 0 0
Sample Output
YESHintChild's path is: (1,1)(1,2)(1,3)(1,2)(1,3)(1,4)(1,5)(4,5) all switches are touched twice except the first one.
Source
看了网上题解,咋思路没有跟我一样的。
题意:一个矩阵,从左上角一格格走到右下角,每到一格就会拨动开关,问最后离开后能否把所有亮着的灯都关上。
思路:从左上角开始往右走到头,往下走一格,然后循环:(再往左走到左二,此时判断这一行的0的个数,然后再从最右进入下一行),如果最后总和是偶数个0就是有解。因为这样走能把整个图都遍历一遍,走到最后有解就是有解,无解就是无解。每一行如果有偶数个0,这一行上就可以经过微调全部调整为0然后满状态进入到下一行,这样循环下去就好。数组都不用开,空间复杂度和时间复杂度都是理论最低值。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <iomanip>
#include <time.h>
#include <set>
#include <map>
#include <stack>
using namespace std;
typedef long long LL;
const int INF=0x7fffffff;
const int MAX_N=10000;
int T;
int n,m,a;
int main(){
cin>>T;
while(T--){
cin>>n>>m;
int num=0;
for(int i=0;i<m;i++){
scanf("%d",&a);
if(a==0)num++;
}//第一行特判
for(int i=1;i<n;i++){
scanf("%d",&a);//每行第一个特判
if(a==0)num++;
for(int j=1;j<m;j++){
scanf("%d",&a);
if(a==1)num++;
}
}
if(num%2==0)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}