POJ 2068 Nim#双人dp博弈
http://poj.org/problem?id=2068
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int dp[25][(1<<13)+5];//dp[i][j]表示轮到第i个人取时,剩j个石头 int n,s,m[25]; int DFS(int pos,int remain) { if(dp[pos][remain]!=-1) return dp[pos][remain];//记忆化 if(remain==0)//必胜态 return dp[pos][remain]=1; dp[pos][remain]=0; for(int i=1;i<=min(m[pos],remain);i++) if(!DFS((pos+1)%n,remain-i))//对方为必败态的话,己方则必胜 dp[pos][remain]=1; return dp[pos][remain]; } int main() { while(scanf("%d",&n)&&n) { scanf("%d",&s); n*=2; for(int i=0;i<n;i++) scanf("%d",&m[i]); memset(dp,-1,sizeof(dp)); printf("%d\n",DFS(0,s)); } return 0; }