ZOJ - 3057 D - Beans Game(三堆威佐夫博弈)

Beans Game

Time Limit: 5 Seconds Memory Limit: 32768 KB

There are three piles of beans. TT and DD pick any number of beans from any pile or the same number from any two piles by turns. Who get the last bean will win. TT and DD are very clever.

Input

Each test case contains of a single line containing 3 integers a b c, indicating the numbers of beans of these piles. It is assumed that 0 <= a,b,c <= 300 and a + b + c > 0.

Output

For each test case, output 1 if TT will win, ouput 0 if DD will win.

Sample Input

1 0 0
1 1 1
2 3 6

Sample Output

1
0
0


转自https://www.cnblogs.com/yuyixingkong/p/3939993.html

博弈题;相对于取石子游戏那题多了一堆;(但,有一点不一样,就是范围!那么就简单多了)
题目大意:有三堆豆子a,b,c(a+b+c<=300)。TT和DD轮流从其中一堆拿走任意个豆子或从其中的两种拿走同样多的豆子,最后一个拿完的获胜。
算法分析:

典型的威佐夫博弈问题是两堆,而此题为三堆。

原本以为这题是要找规律,但是找了一个小时没找到。百度解题报告,发现原来只要逆向推即可。采用递推比较快。

p[i][j][k] = 0表示状态为i,j,k时是必败态,p[i][j][k] = 1为必胜态。

从必败态往上推,能从必败态推出来的就是必胜态,赋值为1,如果是必胜点就不用往上推了。

p[i][j][k]初始化为必败点,如果该点不能从必败点转移过来,该点就是必败点。

由于必败点比较少所以可以节省时间。

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

bool vis[305][305][305];   //卡内存

void fun()
{
    int i,j,k;
    for(i=0;i<=300;i++)
    {
        for(j=0;j<=300;j++)
        {
            for(k=0;k<=300;k++)
            {
                if(vis[i][j][k]==0)
                {
                    int p;
                    for(p=i+1;p<=300;p++)
                        vis[p][j][k]=1;
                    for(p=j+1;p<=300;p++)
                        vis[i][p][k]=1;
                    for(p=k+1;p<=300;p++)
                        vis[i][j][p]=1;
                    for(p=1;p+i<=300&&p+j<=300;p++)
                        vis[p+i][p+j][k]=1;
                    for(p=1;p+j<=300&&p+k<=300;p++)
                        vis[i][p+j][p+k]=1;
                    for(p=1;p+i<=300&&p+k<=300;p++)
                        vis[p+i][j][p+k]=1;
                }
            }
        }
    }
}

int main()
{
    int a,b,c;
    memset(vis,0,sizeof(vis));
    fun();
    while(scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
        printf("%d\n",vis[a][b][c]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/yzm10/p/10328632.html