您的位置:首页 > 其它

uva 1561 - Cycle Game(推理)

2014-08-09 03:13 302 查看
题目链接:uva 1561 - Cycle Game

题目大意:给出一个环,每次从起点开始,可以选择一个权值非0的边移动,移动后减掉权值至少1点。不能移动的为失败。

解题思路:
1:有0的情况,如果有方向离权值为0的边的步数为奇数,则为必胜;否则必败;
2:无0的情况,奇数边必胜;
3:有1的情况,同0的判断一样;
4:无1的情况,只剩偶数边的情况,必败;
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 30;

int N, arr[maxn];

bool check (int k) {
int p = -1;
while (arr[++p] != k);
int q = N;
while (arr[--q] != k);
q = N - 1 - q;
return (p&1) || (q&1);
}

bool judge () {
for (int i = 0; i < N; i++) {
if (arr[i] == 0)
return check(0);
}

if (N&1)
return true;

for (int i = 0; i < N; i++) {
if (arr[i] == 1)
return check(1);
}
return false;
}

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