【ZOJ - 3057】Beans Game (博弈dp)

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

思路:

博弈dp,把所有状态的是否能赢提前求出来。必败态只能到必胜态,所以枚举每一个必败态后的每一个点,都标记为必胜态。

注意这道题数组不能开Int类型要开bool,要不会一直段错误。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define mod (19961993)
using namespace std;
typedef long long ll;
bool dp[302][302][302]={0};
//博弈dp
void init()
{
	for(int i=0;i<=300;i++)
	for(int j=0;j<=300;j++)
	for(int k=0;k<=300;k++)
	{
		if(!dp[i][j][k])
		{
			for(int t=1;t+i<=300;t++)//每次至少选一个  
			dp[t+i][j][k]=1;
			for(int t=1;t+j<=300;t++)
			dp[i][t+j][k]=1;
			for(int t=1;t+k<=300;t++)
			dp[i][j][t+k]=1;
			for(int t=1;t+i<=300&&t+j<=300;t++)
			dp[t+i][t+j][k]=1;
			for(int t=1;t+i<=300&&t+k<=300;t++)
			dp[t+i][j][t+k]=1;
			for(int t=1;t+j<=300&&t+k<=300;t++)
			dp[i][t+j][t+k]=1;
		}
	}
}
int main()
{
	init();
	int a,b,c;
	while(~scanf("%d%d%d",&a,&b,&c))//段错误,空间开的太大,开int是开Bool的4倍, 
	{
		printf("%d\n",dp[a][b][c]); 
	} 
	return 0; 
}

 

你可能感兴趣的:(博弈dp,zoj)