您的位置:首页 > 其它

hdu5601N*M bulbs

2015-12-28 01:12 288 查看

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5601

题意:给你一个n*m的矩阵,矩阵内每一个位置的值是0或1,给定的是初始状态,然后让你从左上角走到右下角,每次都能向着4个方向走,每走到一个格子的时候它原来的值x^=1。问最后是否能从右下角离开时使得整个矩阵全部为0。

分析:其实出题人在解释了题目样例之后还是挺好想的,首先我们能知道想将原来的1变为0的话我们只能通过奇数次,原来为0的话我们通过的次数要为偶数次,这样说好像把题目变麻烦了,但是!如果我们拿着笔在矩阵中画的话很容易就能看出最多同时有两个连在一起的格子会有影响,我们可以想象成是一条长度永远为2的贪吃蛇在地图上走来走去,先把全部的1都"吃"掉,最后走00这种地形,也就是出题人解释样例的那种折叠走,很简单我们的目的点在map
[m],那么我们在走到map
[m]旁边怎么样就能刚好使得所有都为0的时候同时还在(n,m)上呢?很容易想到这和n+m的奇偶性是有关系的,至于哪种是YES哪种是NO就自己随便写两组数据就能分辨出来了。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1010;
const int MAX=151;
const int MOD=1000007;
const int MOD1=100000007;
const int MOD2=100000009;
const int INF=1000000000;
const double EPS=0.00000001;
typedef long long ll;
typedef unsigned long long ull;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int a

;
int main()
{
int i,j,t,n,m,bo,tot;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
bo=tot=0;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++) {
scanf("%d", &a[i][j]);tot+=a[i][j];
}
if ((tot&1)&&!((n+m)&1)) bo=1;
if (!(tot&1)&&((n+m)&1)) bo=1;
if (bo) printf("YES\n");
else printf("NO\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: