您的位置:首页 > 其它

hdoj5601N*M bulbs

2015-12-27 18:48 267 查看


N*M bulbs

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 204    Accepted Submission(s): 114


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.

* 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.

 

Sample Input

1
1 5
1 0 0 0 0

 

Sample Output

YES

HintChild'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)

我们发现操作数跟n+m−1n+m-1n+m−1同奇偶,那是不是当111的个数跟n+m−1n+m-1n+m−1同奇偶是就是YES呢?

答案是肯定的,我们这样看:首先将棋盘黑白染色,就是若(i,j)(i,j)(i,j)格子,若(i+j)(i+j)(i+j)是奇数,那么就是黑格子,否则就是白格子。

我们发现我们可以通过一种操作使得从一个格子走到斜方向的任意一个格子。

这个操作很简单,我们假设一个2∗22*22∗2的棋盘:

1 2

3 4

我们这样走:1−>2−>1−>2−>41->2->1->2->41−>2−>1−>2−>4, 111就直接走到444了,而且不产生任何操作。

也就是同色格子可以互相到达。

然后我们发现如果要操作一个开关,那么最后所在格子颜色一定会改变。

同上面这个例子:

1 2

3 4

假设我们要操作222这个格子。

1−>2−>1−>31->2->1->31−>2−>1−>3

我们成功操作了222这个格子,但是从白格子转到黑格子了。

也就是说

假设格子(n,m)(n,m)(n,m)下面有个格子(n+1,m)(n+1,m)(n+1,m)是最后终点,然而每次操作一个格子需要改变一次颜色。

也就是我们从(1,1)(1,1)(1,1)改变了若干次颜色后,最后颜色一定要和(n+1,m)(n+1,m)(n+1,m)相同。

也就是说111的个数要和(n+1+m)(n+1+m)(n+1+m)同奇偶。

也就是说111的个数要和(n+m−1)(n+m-1)(n+m−1)同奇偶。

否则无解。

#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
int t,i,j,k,n,m,ans;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);ans=0;
for(i=0;i<n;++i){
for(j=0;j<m;++j){
scanf("%d",&k);
if(k)ans++;
}
}
if(((ans&1)&&((n+m-1)&1))||((ans%2==0&&(n+m-1)%2==0))){
printf("YES\n");
}
else {
printf("NO\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdoj5601NM bulbs