您的位置:首页 > 其它

poj2068 Nim (博弈)

2015-07-28 19:27 218 查看

Nim

Time Limit: 1000ms

Memory Limit: 30000KB

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

题目链接

poj2068

题意

初始S个石子,每队n个玩家,相同队伍玩家不相邻,第i个玩家可以取走M[i]个石子,从第一个玩家开始交替操作,取走最后一个石子者输。

题解

定义sg[i][j]为第i号玩家操作时有j个石子剩余的sg函数,对于所有的i,sg[i][0]=1,DP求解。代码如下

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int MAX=(1<<31)-1;
int n,S,M[25];
int sg[25][8200];
int SG(int x,int y)
{
if(sg[x][y]!=-1)
return sg[x][y];
for(int i=1;i<=M[x];i++)
{
if(y-i==0)
break;
if(SG((x+1)%(2*n),y-i)==0)
return sg[x][y]=1;
}
return sg[x][y]=0;
}
int main()
{
while(1)
{

scanf("%d",&n);
if(n==0)
return 0;
scanf("%d",&S);
for(int i=0;i<2*n;i++)
scanf("%d",&M[i]);
memset(sg,-1,sizeof sg);
for(int i=0;i<2*n;i++)
sg[i][0]=1;
if(SG(0,S))
printf("1\n");
else
printf("0\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj