uvalive 5760 Alice and Bob (组合游戏,dp)
2014-08-12 22:07
369 查看
题目链接: http://vjudge.net/problem/viewProblem.action?id=25636
对于>1的堆,必然会被其中一人全部合并。
然后就是二维dp,dp[非1堆的操作数][1堆个数]。
#include <stdio.h>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
#define lson o<<1
#define rson o<<1|1
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
#define INF 200000000
typedef long long ll;
int dp[60][50100];
int dfs(int one,int tot){
if(dp[one][tot]>-1)return dp[one][tot];
if(tot==1)return dp[one][tot]=dfs(one+1,0);
if(one==0)return dp[one][tot]=tot&1;
if(tot>0&&!dfs(one,tot-1))return dp[one][tot]=1;
if(!dfs(one-1,tot))return dp[one][tot]=1;
if(tot&&!dfs(one-1,tot+1))return dp[one][tot]=1;
if(one>=2)
if((tot&&!dfs(one-2,tot+3))||(!tot&&!dfs(one-2,tot+2)))
return dp[one][tot]=1;
return dp[one][tot]=0;
}
int main(){
int t,cs,n;
scanf("%d",&t);
memset(dp,-1,sizeof dp);
dp[0][0]=0;
for(cs=1;cs<=t;cs++){
scanf("%d",&n);
int x,one=0,tot=0;
for(int i=0;i<n;i++){
scanf("%d",&x);
if(x==1)one++;
else
tot+=x+1;
}
if(tot)tot--;
printf("Case #%d: ",cs);
if(dfs(one,tot))printf("Alice\n");
else printf("Bob\n");
}
return 0;
}
对于>1的堆,必然会被其中一人全部合并。
然后就是二维dp,dp[非1堆的操作数][1堆个数]。
#include <stdio.h>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
#define lson o<<1
#define rson o<<1|1
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
#define INF 200000000
typedef long long ll;
int dp[60][50100];
int dfs(int one,int tot){
if(dp[one][tot]>-1)return dp[one][tot];
if(tot==1)return dp[one][tot]=dfs(one+1,0);
if(one==0)return dp[one][tot]=tot&1;
if(tot>0&&!dfs(one,tot-1))return dp[one][tot]=1;
if(!dfs(one-1,tot))return dp[one][tot]=1;
if(tot&&!dfs(one-1,tot+1))return dp[one][tot]=1;
if(one>=2)
if((tot&&!dfs(one-2,tot+3))||(!tot&&!dfs(one-2,tot+2)))
return dp[one][tot]=1;
return dp[one][tot]=0;
}
int main(){
int t,cs,n;
scanf("%d",&t);
memset(dp,-1,sizeof dp);
dp[0][0]=0;
for(cs=1;cs<=t;cs++){
scanf("%d",&n);
int x,one=0,tot=0;
for(int i=0;i<n;i++){
scanf("%d",&x);
if(x==1)one++;
else
tot+=x+1;
}
if(tot)tot--;
printf("Case #%d: ",cs);
if(dfs(one,tot))printf("Alice\n");
else printf("Bob\n");
}
return 0;
}
相关文章推荐
- UVALive - 5088 Alice and Bob's Trip 树形DP
- UVALive 5088 Alice and Bob's Trip(树形DP)
- [UVALive5760] Alice and Bob && 博弈
- UVALive 5760 Alice and Bob
- UVALive 5088 Alice and Bob's Trip(树形DP)
- uva 1484 - Alice and Bob's Trip(树形dp)
- UVA1484 Alice and Bob's Trip(树形dp)
- UVALive - 5088 Alice and Bob's Trip
- Uva 1484 Alice and Bob's Trip(树形dp)
- uva 1484 Alice and Bob's Trip (树形dp)
- UVA 1484 - Alice and Bob's Trip(树形DP)
- UVA 1484 - Alice and Bob's Trip(树形DP)
- uva1484 Alice and Bob's Trip (树形dp)
- UVA 1484 Alice and Bob's Trip (树形dp)
- UVALive - 3882 And Then There Was One (递推[dp])
- UVA 1500 - Alice and Bob(博弈)
- uva-1484(hdu-3660) Alice and Bob's Trip 听说是树形dp
- 玲珑学院OJ 1028 - Bob and Alice are playing numbers 字典树,dp
- UVA 1500-Alice and Bob
- 树形dp-hdu-3660-Alice and Bob's Trip