HDU 6266 Hakase and Nano 【博弈论】

题面:

Hakase and Nano are playing an ancient pebble game (pebble is a kind of rock). There are n packs
of pebbles, and the i-th pack contains ai pebbles. They take turns to pick up pebbles. In each turn,
they can choose a pack arbitrarily and pick up at least one pebble in this pack. The person who
takes the last pebble wins.
This time, Hakase cheats. In each turn, she must pick pebbles following the rules twice continuously.
Suppose both players play optimally, can you tell whether Hakase will win?
Input
The first line contains an integer $T (1 <= T <= 20) $ representing the number of test cases.
For each test case, the first line of description contains two integers \(n(1 <= n <= 106) and d (d = 1 or d = 2)\). If d = 1, Hakase takes first and if d = 2, Nano takes first. n represents the number of pebble
packs.
The second line contains n integers, the i-th integer ai (1 ai 109) represents the number of
pebbles in the i-th pebble pack.
Output
For each test case, print “Yes” or “No” in one line. If Hakase can win, print “Yes”, otherwise, print“No”.

Example

standard inputstandard output
2
3 1
1 1 2Yes
3 2
1 1 2No

题目大意:

有两个人从N个石子堆中拿石子,规则类似Nim游戏。但不一样的是第一个人可以拿两次,第二个人只能拿一次。问最后谁能把石子都拿完。

大致思路:

面对博弈题我的思路是先把简单情况推一遍,然后分析。
1220459-20180525165324788-639001427.jpg

如图,图中A,B表示谁先手,W,L表示在这种情况下,A是否能赢。

所以就有以下规律:

1.当 \(n\) 为 3的 倍数时,若每个石子堆都是1,A先手必输。只有一个数量大于1的石子堆,则B先手A必输

2.当 \(n\) 为3的倍数加一时。若数量大于1的石子堆数量小于等于一。则B先手A必输。

其他情况均为A必胜。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;
int main()
{
    ios::sync_with_stdio(false);
    bool flag=true;
    int t,n,d,a;
    cin>>t;
    while(t--)
    {
        flag=true;
        int cnt=0;
        cin>>n>>d;
        for(int i=0;i<n;++i){
            cin>>a;
            if(a>=2)
                cnt++;
        }
        int x=n%3;
        if(x==0){
            if(cnt==0 && d==1)
                flag=false;
            if(cnt==1 && d==2)
                flag=false;
        }else if(x==1){
            if(cnt<=1 && d==2)
                flag=false;
        }
        if(flag)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/SCaryon/p/9089525.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值