【記憶化搜索】codevs2823鎖妖塔
阿新 • • 發佈:2017-06-07
現在 不想 algo strong printf tchar getch efi align
[codevs2823]鎖妖塔
Description
提示:1樓不需要再下了,N層樓只需要下N-1層。並且在第N層樓到N-1層時必須為初始速度。
瑣妖塔會在一會兒後倒塌。大量妖魔湧出塔去,塔內的樓梯都擠滿了人(哦,錯了,是妖),(那他們怎麽不飛下去--)要求是,景天一行一定要下塔,瑣妖塔一共N層,但是他突然大發慈悲,覺得妖怪是無辜,所以他不想踩死這些妖魔,所以他的速度最多比妖怪速度大K(否則會踩死妖怪的),並且速度不能比妖怪們慢,否則會被踩死。瑣妖塔一共有N層,並且每層怪物逃跑的速度都不相同,景天每下一層,可以選擇將他的速度加快一個單位或者減慢一個單位或者保持原來的速度不變。並且他下每一層的速度之和除以(N-1)要盡量大。當然跑下樓時他一定要活著。
現在景天剛拿到鎮妖劍,頭有點熱,不能思考了,請你編個程序幫幫他吧!
Input Description
第一行,三個整數N,V(初始速度),K(最多比其他妖快的速度值)
第二行,N-1個整數,分別代表從第二層到第N層的妖怪的速度
其中2〈=N〈=100,0〈=K〈=100,1〈=V〈=100。
Output Description
若能下樓,輸出速度之和除以(N-1),保留兩位小數。
若不能,那就仰天大吼一聲,輸出“REN JIU SHI BU NENG REN CI!”(不含引號)
Sample Input
Input1
3 3 2
2 2
Input2
3 3 0
2 2
Output1
3.50
Output2
REN JIU SHI BU NENG REN CI!
試題分析:記憶化搜一下就可以了……
#include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<stack> #include<vector> #include<algorithm> //#include<cmath> using namespace std; const int INF = 9999999; #define LL long long inline int read(){ int x=0,f=1;char c=getchar(); for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1; for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘; return x*f; } int N,V,K; int a[101]; int f[101][101]; bool vis[101][101]; int ans; bool search(int n,int v){ if(vis[n][v]) return f[n][v]; vis[n][v]=true; if(n==1) return 1; if(v-a[n]>K||v<a[n]) return 0; return f[n][v]=search(n-1,v+1)||search(n-1,v)||search(n-1,v-1); } void GA(int n,int v){ if(n==1) return; ans+=v; if(f[n-1][v+1]) GA(n-1,v+1); else if(f[n-1][v]) GA(n-1,v); else GA(n-1,v-1); } int main(){ N=read(),V=read(),K=read(); for(int i=2;i<=N;i++) a[i]=read(); int p=V; for(int i=N;i>=2;i--){ if(p>K+a[i]) { cout<<"REN JIU SHI BU NENG REN CI! "; return 0; } if(p>1) p--; } search(N,V); if(!f[N][V]) cout<<"REN JIU SHI BU NENG REN CI! "; else{GA(N,V);printf("%.2lf",(double)ans/(N-1));} return 0; }
【記憶化搜索】codevs2823鎖妖塔