ZOJ 3452 Doraemon's Stone Game(高级博弈)

Doraemon is playing a game with Dorami. Initially there are piles of stones on the table. Each pile consists of at most 2 stones. Each stone may be black or white. Doraemon and Dorami take turn to take away the stones, the rule is as follows:

  • Doraemon can only take away a white stone each time.
  • Dorami can only take away a black stone each time.
  • If the stone in the bottom is taken away, the whole pile is removed.
  • The first one who can't take away any stone lose. The other one is the winner

Now the piles of stones on the table are known, Doraemon wants to know if he can win. Can you help him?

Input

There are multiple cases (about 45000). 
For each case, the first line is an integer N (1 ≤ N ≤ 6). Then N lines follows. The i-th line contains a string Si and an integer number ai (1 ≤ ai ≤ 1000000000). Si represents a pile of stones. The characters from left to right correspond to stones from top to bottom. The character 'w' means that stone is white and 'b' means that stone is black. The integer ai means there are ai piles of stones represented by Si. It's guaranteed that Si ≠ Sj when i ≠ j.

<h4< dd="">
Output

For each case, output two words separated by a space in one line. The first word should be "win" if Doraemon can win the game if he makes move first, otherwise it should be "lose". The second word should be "win" if Doraemon can win the game if Dorami makes move first, otherwise it should be "lose".

<h4< dd="">
Sample Input
 
2
w 1
b 1
2
b 1
wb 1
<h4< dd="">
Sample Output
 
lose win
lose lose

 【题解】  一道博弈题,两个人玩取石子游戏,石子有黑白两种,有ai堆石子,每堆最多只有两个石子,Doraemon每次只能拿白色石子,另一个人只能拿黑色石子,两人轮流操作,谁先没有石子拿,谁就输了,还有一个很重要的条件:石子是从上往下放的,输入中用从左到右表示石子从上往下的顺序,注意,如果下面的石子被拿走了,则意味着整个堆就没了(题意是这么说的,不知道咋没的)。

 

  根据题意,我们可以想象,我们要赢,不让对方赢,那么如果对方拿走一个w,那我就要拿走一个b来维持这种平衡,使得整个平衡不会出现偏移,最后抵消剩下wb或者bw,现在,对于剩下的wb(w和b的个数一定是不一样的,因为一样的都抵消了),如果Doraemon先拿,那么他就可以拿(n+1)/2次,而如果Doraemon后拿,则他可以拿n/2次,所以,对剩下的bw原理也是一样的,这样操作完就剩下两个人的操作次数了,等价于谁的可操作次数多,则谁赢。

【AC代码】

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
char s[3];
ll a[50000];
int m;

int main()
{
    while(~scanf("%d",&m))
    {
        ll ww=0,bb=0,wb=0,bw=0;
        for(int i=1;i<=m;++i)
        {
            scanf("%s %lld",s,&a[i]);//注意是ll  不然会爆
            int len=strlen(s);
            if(len==2)
            {
                if(s[0]=='w'&&s[1]=='w') ww+=2*a[i];//乘2是因为两个w可以操作2次 下同
                else if(s[0]=='w'&&s[1]=='b') wb+=a[i];
                else if(s[0]=='b'&&s[1]=='w') bw+=a[i];
                else if(s[0]=='b'&&s[1]=='b') bb+=2*a[i];
            }
            else if(len==1)
            {
                if(s[0]=='w') ww+=a[i];
                else bb+=a[i];
            }

            if(ww==bb) ww=bb=0;
            else if(ww>bb) ww-=bb,bb=0;//注意bb设为0  下同
            else if(ww<bb) bb-=ww,ww=0;

            if(wb==bw) wb=bw=0;
            else if(wb>bw) wb-=bw,bw=0;
            else if(bw>wb) bw-=wb,wb=0;
        }

        ll ans1=ww+(wb+1+2*bw)/2;//A先拿
        ll ans2=bb+(bw+2*wb)/2;

        if(ans1>ans2) printf("win ");
        else printf("lose ");

        ans1=ww+bw+wb/2;//A后拿
        ans2=bb+wb+(bw+1)/2;

        if(ans1>=ans2) printf("win\n");
        else printf("lose\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值