dp博弈______Nim( poj 2068 )

Description

Let's play a traditional game Nim. You and I are seated across a table and we have a hundred stones on the table (we know the number of stones exactly). We play in turn and at each turn, you or I can remove on to four stones from the heap. You play first and the one who removed the last stone loses. 
In this game, you have a winning strategy. To see this, you first remove four stones and leave 96 stones. No matter how I play, I will end up with leaving 92 - 95 stones. Then you will in turn leave 91 stones for me (verify this is always possible). This way, you can always leave 5k+1 stones for me and finally I get the last stone, sigh. If we initially had 101 stones, on the other hand, I have a winning strategy and you are doomed to lose. 

Let's generalize the game a little bit. First, let's make it a team game. Each team has n players and the 2n players are seated around the table, with each player having opponents at both sides. Turn around the table so the two teams play alternately. Second, let's vary the maximum number of stones each player can take. That is, each player has his/her own maximum number of stones he/she can take at each turn (The minimum is always one). So the game is asymmetric and may even be unfair. 

In general, when played between two teams of experts, the outcome of a game is completely determined by the initial number of stones and the maximum number of stones each player can take at each turn. In other words, either team has a winning strategy. 

You are the head-coach of a team. In each game, the umpire shows both teams the initial number of stones and the maximum number of stones each player can take at each turn. Your team plays first. Your job is, given those numbers, to instantaneously judge whether your team has a winning strategy. 

Incidentally, there is a rumor that Captain Future and her officers of Hakodate-maru love this game, and they are killing their time playing it during their missions. You wonder where the stones are? Well, they do not have stones but do have plenty of balls in the fuel containers! 

Input

The input is a sequence of lines, followed by the last line containing a zero. Each line except the last is a sequence of integers and has the following format. 

n S M1 M2 . . . M2n 

where n is the number of players in a team, S the initial number of stones, and Mi the maximum number of stones ith player can take. 1st, 3rd, 5th, ... players are your team's players and 2nd, 4th, 6th, ... the opponents. Numbers are separated by a single space character. You may assume 1 <= n <= 10, 1 <= Mi <= 16, and 1 <= S < 2^13. 

Output

The output should consist of lines each containing either a one, meaning your team has a winning strategy, or a zero otherwise. 

Sample Input

1 101 4 4
1 100 4 4
3 97 8 7 6 5 4 3
0

Sample Output

0
1
1



题意:

有两个队伍A,B,每个队伍有N个人,交叉坐。即是A(1,3,5,7.....)B(2,4,6,8....)。告诉你每个mi(1<=i<=2n)。

现在有一堆个数为S的石堆,从第1个人开始拿石头,因为是交叉坐所以也就相当于两队轮流拿石头。注意:第i个人拿石头最少拿1个最多拿mi个,拿完石堆中最后一个石头的输。问:A队有没有必胜策略?


分析:

嗯因为数据都不是很大,我们可以用记忆化搜索来求出整个博弈图。如果后继状态有后手必胜(0)那么该状态为先手必胜(1)。如果后继状态全部为先手必胜(1).那么该点状态为后手必胜(0)。当石堆个数为0的时候状态为先手必胜。也就是图的边界。这样用回溯就可以求出所有状态是先手必胜还是后手必胜。

状态表示为  DP[ i ][ j ] 表示轮到第j个人拿,当前还有 i 个石头。


代码:

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

int dp[9000][20];
int n,s;
int a[20];
int getdp(int num,int index)
{
     if(index == 2*n) return getdp(num,0);  //最后一个人拿了又要轮到第一个人拿。
    if(dp[num][index] != -1) return dp[num][index];
   
    if(num == 0) return dp[num][index] = 1;
    for(int i = 1 ; i <= a[index] ; i ++)
    {
        if(num < i) break;
        if(getdp(num - i,index+1) == 0)
            return dp[num][index] = 1;
    }
    return dp[num][index] = 0;
}
int main()
{
    while(scanf("%d",&n),n)
    {
        scanf("%d",&s);
        for(int i = 0 ; i < 2*n ; i ++)
            scanf("%d",&a[i]);
        memset(dp,-1,sizeof(dp));
        //for(int i = 0 ; i <= s ; i ++)
            printf("%d\n",getdp(s,0));
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值