题目链接:https://nanti.jisuanke.com/t/31454
分析: dfs + 记忆化搜索 , 0 表示 Bad , 1 表示 Normal , 2 表示Good
代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define mod 100000+7
#define lowbit(x) (x&(-x))
#define mem(a,b) memset(a,b,sizeof(a))
#define FRER() freopen("in.txt","r",stdin);
#define FREW() freopen("out.txt","w",stdout);
using namespace std;
const int maxn = 10000 + 7, inf = 0x3f3f3f3f;
int a[maxn],b[maxn],c[maxn];
int dp[maxn][200+7];
map<int,int>id;
int n,s,k,l,tot=0;
int dfs(int pos,int u){
if(pos==n+1){
if(u>=k) return 2;
else if(u>l) return 1;
else return 0;
}
if(dp[pos][id[u]]!=-1) return dp[pos][id[u]];
if(pos&1){
int tmp = 0;
if(a[pos]!=0) tmp = max(tmp,dfs(pos+1,min(u+a[pos],100)));
if(b[pos]!=0) tmp = max(tmp,dfs(pos+1,max(u+b[pos],-100)));
if(c[pos]!=0) tmp = max(tmp,dfs(pos+1,-u));
return dp[pos][id[u]] = tmp;
}else{
int tmp = 2;
if(a[pos]!=0) tmp = min(tmp,dfs(pos+1,min(u+a[pos],100)));
if(b[pos]!=0) tmp = min(tmp,dfs(pos+1,max(u+b[pos],-100)));
if(c[pos]!=0) tmp = min(tmp,dfs(pos+1,-u));
return dp[pos][id[u]] = tmp;
}
}
int main()
{
//FRER();
scanf("%d%d%d%d",&n,&s,&k,&l);
for(int i=1;i<=n;i++){
scanf("%d%d%d",&a[i],&b[i],&c[i]);
b[i] = -b[i];
}
for(int i=-100;i<=100;i++)
id[i] = ++tot;
mem(dp,-1);
int ans = dfs(1,s);
if(ans == 2) printf("Good Ending\n");
if(ans == 1) printf("Normal Ending\n");
if(ans == 0) printf("Bad Ending\n");
return 0;
}