杭电5601 N*M bulbs

来源:互联网 发布:买家如何进入淘宝客 编辑:程序博客网 时间:2024/06/08 11:43

N*M bulbs

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 331    Accepted Submission(s): 186


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.
 

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.

T10
N,M1000
 

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.
 

Sample Input
11 51 0 0 0 0
 

Sample Output
YES
Hint
Child'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
BestCoder Round #67 (div.2)
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5609 5608 5607 5605 5604 
具体方法就不说了,首先可以看我上一篇博客,然后具体思路是,按照上边的,n*m的只需要把他来成一条线就成n*1的了,但是要分情况稍微考虑
1.假设前n-1行有偶数行,就像图2一样,那么最后一行的起点就是最左边就类似上一题,只要前n-1行能完成关闭状态(即处于关闭状态的有偶数个)加上最后一行能完成(关闭状态的有偶数个)最后就能达到目的,或者前n-1行不能,但是最后一行也不能,这样仍然可以全部使其处于关闭状态。
2.假设前n-1行有奇数行,就像图1一样,最后一行的起点为最右边,那么便需转换思路,就是说是从最后一行最后一个开始往前走,由于题目上说一定要走到最后一个,那么便是往前走,再拐回来,那么判断方法就变了,变为,如果最后一行处于开着的有奇数个那么最后一行就能完成全部变为关闭状态,类比上一个状态,如果前n-1行能完成,并且最后一行也能完成,那么最后就能完成,即前n-1行处于关闭状态的有偶数个,最后一行处于开着状态的有奇数个,还有一种是前n-1行不能完成,但是最后一行也不能完成,那么就能完成了,即前n-1行处于关闭状态的有奇数个,最后一行处于开着状态的有偶数个。
当然也可以按列走,怎么走,只要自己喜欢就行。

最后附上丑陋的代码:

#include<string.h>#include<algorithm>#include<stdio.h>using namespace std;int num,i,j,k,l,m,n,cnt;int main(){scanf("%d",&k);while(k--){scanf("%d%d",&m,&n);num=cnt=0;for(i=0;i<m-1;i++)for(j=0;j<n;j++){scanf("%d",&l);if(l==0)num++;}for(j=0;j<n;j++){scanf("%d",&l);if(l==0)cnt++;}if(m%2&&(num+cnt)%2==0)printf("YES\n");else if(m%2==0&&(num+(n-cnt))%2==1)printf("YES\n");else printf("NO\n");}}


0 0
原创粉丝点击