hdu 5601 N*M bulbs 找规律

N*M bulbs

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


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
  
  
1 1 5 1 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

本题是在hdu 5600 N bulbs 题的扩展,在上题的基础上进一步推倒,可以解决问题。

首先除掉所有除(0,0)点以外的所有1,具体步骤就是从(0.0)出发,然后到达那个1的位置,然后回到(0,0)的倒数一步【注意是倒数一步,不要直接回到(0,0)】,因为这样才有周期。一个周期消除一个1,。每个周期其实就只改变(0,0)和刚刚消除1的那点的坐标,而最终(0,0)的具体值就是周期的奇偶有关,奇数相反,偶数不变。最后一步就是从(0,0)到目的地(n,m),这时就可以用hdu 5600 N bulbs 题的结论,判断路径上的0的个数。


Source
BestCoder Round #67 (div.2)


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
int main()
{
	int i,j,k,n,m,t,tt;
	int cnt,ct;
	int tmp;
	scanf("%d",&tt);
	while(tt--){
		cnt = 0;
		scanf("%d%d",&n,&m);
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				 scanf("%d",&t);
				 if(i==0&&j==0){  //起始点不能算在cnt中,先不考虑,因为每次操作
				 //是从(0,0)到除开(0,0)点其他的1的位置,然后回到(0,0)的倒数第一步
				 //而如果清除原点按以上的方法,会发现没有(0,0)的倒数第一步,于是我们先除掉除开(0.0)以外的所有1) 
				 	tmp = t;
					 continue;	
				 }
				 cnt+=t;
			}
		}
		tmp = (cnt%2)?(!tmp):tmp;  //消除偶数的1,(0,0)的值不变,反之变. 
		if((m+n-1-tmp)%2==0){  // m+n-1是从(0,0)到(n,m)的步数,除开(0,0)不确定,其他的所有点都是0了,然后根据以前消除了多少个1来确定(0,0)现在的值
		// (m+n-1-tmp)%2==0是用到	5600	 N bulbs 题的性质,在一维的情况下,能不能成功就看0的个数。 
				printf("YES\n");
			}
			else printf("NO\n");
	}
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值