ZOJ 3057 Beans Game 【DP+博弈】

来源:互联网 发布:怎么安装visio软件 编辑:程序博客网 时间:2024/05/29 02:03

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

题意:
三维数组分别表示,取三个物品的数量。1.那么之前出现过某两物品数相同,且另一物品数小于当前第三物品数的状态为必胜态,2.如果一物品数相同,且另两物品数大于前面的另外两个物品数,且差相同,则为必胜态,不是这两种必胜态,则为必败态。

正推的效率远不及倒推的效率。
记忆化搜索会炸内存,甚至int都不行,必须用bool。

#include<iostream>  #include<cstdio>  #include<ctime>  #include<cstring>  #include<cmath>  #include<algorithm>  #include<cstdlib>  #include<vector>  #define C    240  #define TIME 10  #define inf 1<<25  #define LL long long  using namespace std;  bool dp[301][301][301]={0};  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 r=1;r+i<=300;r++)                          dp[i+r][j][k]=1;                      for(int r=1;r+j<=300;r++)                          dp[i][j+r][k]=1;                      for(int r=1;r+k<=300;r++)                          dp[i][j][k+r]=1;                      for(int r=1;r+j<=300&&r+i<=300;r++)                          dp[i+r][j+r][k]=1;                      for(int r=1;r+j<=300&&r+k<=300;r++)                          dp[i][j+r][k+r]=1;                      for(int r=1;r+k<=300&&r+i<=300;r++)                          dp[i+r][j][k+r]=1;                  }              }          }      }  }  int main(){      int a,b,c;      Init();      while(cin>>a>>b>>c)          cout<<dp[a][b][c]<<endl;      return 0;  }  
原创粉丝点击