無法拯救我的菜----徐州網路賽 B. BE, GE or NE
阿新 • • 發佈:2018-12-09
不會,看的題解。。。。。菜。。。 可以理解為一個人想讓最終分數儘可能高,另一個想讓最終分數儘可能低,然後根據每個問題雙向dp
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1005;
int dp[N][205];
int a[N],b[N],c[N];
int n,m,k,l;
int dfs(int id,int score)
{
if(id == n + 1)
return score;
if (dp[id][score] != -1)
return dp[id][score];
if(id & 1){
int res = -1;
if(a[id])
res = max(res,dfs(id + 1,min(200,score + a[id])));
if(b[id])
res = max(res,dfs(id + 1,max(0,score - b[id])));
if(c[id])
res = max(res,dfs(id + 1,200 - score));
dp[id][score] = res;
}else{
int res = 300;
if(a[id])
res = min(res,dfs(id + 1,min(200,score + a[id])));
if(b[id])
res = min(res,dfs(id + 1,max(0,score - b[id])));
if(c[id])
res = min(res,dfs(id + 1,200 - score));
dp[id ][score] = res;
}
return dp[id][score];
}
int main()
{
while(~scanf("%d %d %d %d",&n,&m,&k,&l))
{
memset(dp,-1,sizeof(dp));
for(int i = 1;i <= n;++i)
{
scanf("%d %d %d",&a[i],&b[i],&c[i]);
}
int ans = dfs(1,m + 100);
if(ans >= k + 100) printf("Good Ending\n");
else if(ans <= l + 100) printf("Bad Ending\n");
else printf("Normal Ending\n");
}
return 0;
}
用記憶化搜尋處理出來每個狀態的勝負情況 因為每個人都會選擇最優的,因此記憶化搜尋的過程其實就是在模擬兩個人每一步決策所帶來的勝負情況, 只要返回一個必勝,就直接返回(因為會選擇最優) 然後在沒有返回必勝的狀態下,有平局就選擇平局,沒有平局就只能輸了
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1005;
int dp[N][205];
int a[N],b[N],c[N];
int n,m,k,l;
int dfs(int id,int score)
{
if(id == n + 1){
if(score >= k) return 1;
else if(score <= l) return 3;
else return 2;
}
int &ans = dp[id][score + 100];
if(ans != -1) return ans;
bool flag = false;
if(a[id]){
int tmp = dfs(id + 1,min(100,score + a[id]));
if(id & 1){
if(tmp == 1) return 1;else ans = 3;
}else{
if(tmp == 3) return 3;else ans = 1;
}
if(tmp == 2) flag = true;
}
if(b[id]){
int tmp = dfs(id + 1,max(-100,score - b[id]));
if(id & 1){
if(tmp == 1) return ans = 1;else ans = 3;
}else{
if(tmp == 3) return ans = 3;else ans = 1;
}
if(tmp == 2) flag = true;
}
if(c[id]){
int tmp = dfs(id + 1,-1 * score);
if(id & 1){
if(tmp == 1) return ans = 1;else ans = 3;
}else{
if(tmp == 3) return ans = 3;else ans = 1;
}
if(tmp == 2) flag = true;
}
if(flag) return ans = 2;
return ans;
}
int main()
{
while(~scanf("%d %d %d %d",&n,&m,&k,&l))
{
memset(dp,-1,sizeof(dp));
for(int i = 1;i <= n;++i)
{
scanf("%d %d %d",&a[i],&b[i],&c[i]);
}
int ans = dfs(1,m);
if(ans == 1) printf("Good Ending\n");
else if(ans == 3) printf("Bad Ending\n");
else printf("Normal Ending\n");
}
return 0;
}