题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3057
题面:
Beans Game Time Limit: 5 Seconds Memory Limit: 32768 KBThere 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.如果一物品数相同,且另两物品数大于前面的另外两个物品数,且差相同,则为必胜态,不是这两种必胜态,则为必败态。
同样的想法,不同实现方法,效率差了十倍。蠢蠢的我从后往前搜,3000+ms,大神直接从前往后推,300+ms....还是要多从别人的代码中学习技巧和思路!
解法一:
#include <iostream> #include <cstdio> #include <map> #include <cstring> #define INF 1e9 using namespace std; bool win[305][305][305]; int max(int a,int b) { return a>b?a:b; } int min(int a,int b) { return a<b?a:b; } void init() { win[0][0][0]=0; for(int i=0;i<=300;i++) { for(int j=0;j<=300;j++) { for(int k=0;k<=300;k++) { if(!win[i][j][k]) { for(int m=300-i;m>0;m--)win[i+m][j][k]=1; for(int m=300-j;m>0;m--)win[i][j+m][k]=1; for(int m=300-k;m>0;m--)win[i][j][k+m]=1; for(int m=min(300-i,300-j);m>0;m--)win[i+m][j+m][k]=1; for(int m=min(300-i,300-k);m>0;m--)win[i+m][j][k+m]=1; for(int m=min(300-j,300-k);m>0;m--)win[i][j+m][k+m]=1; } } } } } int main() { init(); int a,b,c; while(~scanf("%d%d%d",&a,&b,&c)) { if(win[a][b][c]) printf("1\n"); else printf("0\n"); } return 0; }
解法二:
#include <iostream> #include <cstdio> #include <map> #include <cstring> #define INF 1e9 using namespace std; bool win[305][305][305]; int max(int a,int b) { return a>b?a:b; } int min(int a,int b) { return a<b?a:b; } void init() { int sum,minn,maxn,mid; bool flag; win[0][0][0]=0; for(int i=0;i<=300;i++) { for(int j=i;j<=300;j++) { for(int k=j;k<=300;k++) { win[i][j][k]=0; flag=false; for(int m=0;m<i;m++) { if(win[m][j][k]==0) { flag=true; win[i][j][k]=1; break; } } if(flag)continue; for(int m=0;m<j;m++) { minn=min(i,m); maxn=max(i,m); if(win[minn][maxn][k]==0) { flag=true; win[i][j][k]=1; break; } } if(flag)continue; for(int m=0;m<k;m++) { if(m<=i) { if(win[m][i][j]==0) { flag=true; win[i][j][k]=1; break; } } else if(m>=i&&m<=j) { if(win[i][m][j]==0) { flag=true; win[i][j][k]=1; break; } } else { if(win[i][j][m]==0) { flag=true; win[i][j][k]=1; break; } } } if(flag)continue; for(int m=i;m>0;m--) { if(win[i-m][j-m][k]==0) { flag=true; win[i][j][k]=1; break; } else { maxn=max(j,k-m); minn=min(j,k-m); if(win[i-m][minn][maxn]==0) { flag=true; win[i][j][k]=1; break; } } } if(flag)continue; for(int m=j;m>0;m--) { sum=i+j-m+k-m; minn=min(i,j-m); maxn=max(i,k-m); mid=sum-minn-maxn; if(win[minn][mid][maxn]==0) { flag=true; win[i][j][k]=1; break; } } } } } } int main() { init(); int a,b,c; int minn,maxn,mid,sum; while(~scanf("%d%d%d",&a,&b,&c)) { sum=a+b+c; minn=min(a,b); minn=min(minn,c); maxn=max(a,b); maxn=max(maxn,c); mid=sum-maxn-minn; if(win[minn][mid][maxn]) printf("1\n"); else printf("0\n"); } return 0; }