洛谷P1288 取数游戏II(博弈)

洛谷P1288 取数游戏II

先手必胜的条件需要满足如下中至少 \(1\) 条:

  • 从初始位置向左走到第一个 \(0\) 的位置,经过边的数目为偶数(包含 \(0\) 这条边)。
  • 从初始位置向右走到第一个 \(0\) 的位置,经过边的数目为偶数(包含 \(0\) 这条边)。

否则先手必败。

#include<stdio.h>
#include<string.h>

using namespace std;

const int maxn = 25;
int a[maxn], n, ans;

int main()
{
    scanf("%d", &n);
    ans = 0;
    for(int i = 1; i <= n; i++){
        scanf("%d", &a[i]);
    }
    for(int i = 1; i <= n; i++){
        if(a[i] == 0){
            if(i % 2 == 0) ans = 1;
            break;
        }
    }
    for(int i = n; i >= 1; i--){
        if(a[i] == 0){
            if((n + 1 - i) % 2 == 0) ans = 1;
            break;
        }
    }
    if(ans == 1) puts("YES");
    else printf("NO");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/solvit/p/11402081.html