poj 2068 Nim(博弈dp)


Nim

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 1403   Accepted: 791

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

Source

Japan 2001

题目:有两个队从石堆中去石子,每个队有n个人,每个人每次取石子的数量有限制,然后

取最后一块的人所在的队伍输。现在问第一队(成员为1,3,5,7...)是否能够赢得比赛。

思路 :dp[i][j]表示第i个人取,石堆剩余 j 块石头。当j为0的时候,没有石头,这时候是胜,为1。

后继中有必败态的为必胜态。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int M=1<<14;
const int maxn=50;

int dp[maxn][M],a[maxn],tol,n;

void initial()
{
    memset(dp,-1,sizeof(dp));
}

void input()
{
    scanf("%d",&tol);
    for(int i=0;i<2*n;i++) scanf("%d",&a[i]);
}

int DP(int x,int num)
{
    if(dp[x][num]!=-1)  return dp[x][num];
    if(num==0)  return dp[x][num]=1;
    dp[x][num]=0;
    for(int i=1;i<=a[x] && i<=num;i++)
        if(!DP((x+1)%(2*n),num-i))
            dp[x][num]=1;
    return dp[x][num];
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)  break;
        initial();
        input();
        printf("%d\n",DP(0,tol));
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 22:58:31

poj 2068 Nim(博弈dp)的相关文章

POJ 2068 Nim#双人dp博弈

http://poj.org/problem?id=2068 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int dp[25][(1<<13)+5];//dp[i][j]表示轮到第i个人取时,剩j个石头 int n,s,m[25]; int DFS(int pos,int remain) { if(dp

UVA 1559 - Nim(博弈dp)

UVA 1559 - Nim 题目链接 题意:一开始有s个石子,2n个人轮流取石子,每个人有个最大能取数目,2n个人奇数一队,偶数一队,取到最后一个石子的队输,问谁赢 思路:记忆化搜索,每个人取的时候对应的后继状态如果有一个必败态,则该状态为必胜态,如果都是必胜态,则该状态为必败态 代码: #include <stdio.h> #include <string.h> int n, s, m[25], dp[25][10005]; int dfs(int now, int state

POJ 2068 Nim (dp博弈)

题意: 共n轮,s个石头和两队人,两队人轮流拿,第i轮两队分别只能拿1~M[(2*i-1)%(2*n)]和1~M[(2*i)%(2*n)]个石头,拿到最后那个石头的队输: 就拿最后一组样例来说,循环中共3轮,共97个石头,第一轮两队分别拿不超过8个和7个石头,第二轮6个和5个,第三轮4个和3个,然后又是8个和7个...... Input n S M[1]  M[2] . . . M[2*n] 1 <= n <= 10, 1 <= Mi <= 16, and 1 <= S &l

POJ 2068 Nim

链接: http://poj.org/problem?id=2068 题意: 传统的Nim游戏由两名玩家进行,在一堆石头中,双方轮流取走任意合法数量块石头,取走最后一块石头的玩家落败. 多人Nim游戏将参赛人数拓展至两个队伍,每支队伍有n名队员交错入座,单次分别能最多取走Mi块石头,取走S块石头中的最后一块的队伍失败, 求第一支队伍是否有必胜策略? 题解: dp[i][j]表示第i个人取,还有j块石头 . 当j为0的时候,没有石头,这时候是胜,为1. 后继中有必败态的为必胜态. 代码: 31 i

POJ 2068 Nim 组合游戏

题目大意:有一堆石子,两伙人,围在一起坐,坐的顺序是ABABABAB....每一个人最多能取a[i]个石子,取走最后一个石子的就输了.问谁能赢. 思路:朴素的组合游戏判定问题,这个题给了数据范围,可以进行记忆化搜索.f[i][j]为还剩下i个石子,到了第j个人的时候的状态,然后记忆化一下. CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #

POJ 2234 Matches Game(Nim博弈裸题)

Description Here is a simple game. In this game, there are several piles of matches and two players. The two player play in turn. In each turn, one can choose a pile and take away arbitrary number of matches from the pile (Of course the number of mat

POJ Cutting Game(Nim博弈+grundy数)

Description Urej loves to play various types of dull games. He usually asks other people to play with him. He says that playing those games can show his extraordinary wit. Recently Urej takes a great interest in a new game, and Erif Nezorf becomes th

ACM学习历程—HDU 3915 Game(Nim博弈 &amp;&amp; xor高斯消元)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所有xor和为0. 那么自然变成了n个数里面取出一些数,使得xor和为0,求取法数. 首先由xor高斯消元得到一组向量基,但是这些向量基是无法表示0的. 所以要表示0,必须有若干0来表示,所以n-row就是消元结束后0的个数,那么2^(n-row)就是能组成0的种数. 对n==row特判一下. 代码:

POJ 1384 Piggy-Bank 背包DP

所谓的完全背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 其实和一般01背包没多少区别,不过数量可以无穷大,那么就可以利用一个物品累加到总容量结尾就可以了. 本题要求装满的,故此增加个限制就可以了. #include <stdio.h> #include <stdlib.h> #include <string.h> inline int min(int a, int b) { return a < b? a : b; } c