ZOJ - 3964 博弈问题

Alice and Bob are playing yet another game of stones. The rules of this game are as follow:

The game starts with n piles of stones indexed from 1 to n. The i-th pile contains ai stones and a special constraint indicated as bi.
The players make their moves alternatively. The allowable moves for the two players are different.
An allowable move of Bob is considered as removal of some positive number of stones from a pile.
An allowable move of Alice is also considered as removal of some positive number of stones from a pile, but is limited by the constraint bi of that pile.
If bi = 0, there are no constraints.
If bi = 1, Alice can only remove some odd number of stones from that pile.
If bi = 2, Alice can only remove some even number of stones from that pile.
Please note that there are no constraints on Bob.
The player who is unable to make an allowable move loses.
Alice is always the first to make a move. Do you know who will win the game if they both play optimally?

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1 ≤ n ≤ 105), indicating the number of piles.

The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 109), indicating the number of stones in each pile.

The third line of each test case contains n integers b1, b2, …, bn (0 ≤ bi ≤ 2), indicating the special constraint of each pile.

It is guaranteed that the sum of n over all test cases does not exceed 106.

We kindly remind you that this problem contains large I/O file, so it’s recommended to use a faster I/O method. For example, you can use scanf/printf instead of cin/cout in C++.

Output
For each test case, output “Alice” (without the quotes) if Alice will win the game. Otherwise, output “Bob” (without the quotes).

与公平博弈不同,这里有 bi 的限制,那么我们应该怎样去转换呢?
先考虑bi==1|| bi==2的特殊情况:
① 当 bi==2&& ai%2==1 ,Alice最优方案不能取完,自然 bob 胜;
② 当存在>=2 的 bi==2或者b1==1&&ai>1时,BOB 也可以胜利,这里情况比较多;
③只有一个bi==2时,那么A必须先将该堆取完,不然B就可以先从该堆取一个,然后A不能取完,那么此时就是n-1 堆,B先手的NIM博弈
④:当只有一个bi==1&&ai>1时,A同样应该尽量取完||剩一个,那么也转换成了NIM博弈

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 200005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}
int a[maxn];
int b[maxn];
int cnt[3];


int main()
{
    //ios::sync_with_stdio(false);
    int t;
    //cin>>t;
    scanf("%d",&t);
    while(t--){
        int n;cin>>n;
        memset(cnt,0,sizeof(cnt));
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)scanf("%d",&b[i]);
        bool flag=false;
        int cnt=0;
        for(int i=1;i<=n;i++){
            if(a[i]%2&&b[i]==2)flag=true;
            if((a[i]>1&&b[i]==1)||(a[i]%2==0&&b[i]==2)){
                cnt++;
                if(a[i]%2==0&&b[i]==1){
                    a[i]=1;
                }
                else a[i]=0;
            }
        }
        if(flag||cnt>1){
            cout<<"Bob"<<endl;
            continue;
        }
       // else {
        int ans=0;
        for(int i=1;i<=n;i++){
            ans^=a[i];
        }
        if(cnt==0){
            if(ans)cout<<"Alice"<<endl;
            else cout<<"Bob"<<endl;
        }
        else {
            if(ans)cout<<"Bob"<<endl;
            else cout<<"Alice"<<endl;
        }
 //   }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值