Hdu 4405 Aeroplane chess - 简单DP
2013-09-05 21:01
405 查看
题目大意:
问从0到n所花费时间平均时间。每次有投骰子,投到几就走几步。当然了,还有近道。
题目分析:
假设现在在i,那么接下来有六种可能的走法,分别是:
i到i+1,在由i+1到结束
i到i+2,在由i+2到结束
i到i+3,在由i+3到结束
i到i+4,在由i+4到结束
i到i+5,在由i+5到结束
i到i+6,在由i+6到结束
其中每一个可能的走法发生的概率为n为1/6。那么不妨定义d(i),表示从i走到结束的期望。
那么有下面的等式:
d(i-1) = sum((d(i+j)+1)*p) 其中j ∈[0,6]。
当i+j >= n时,只需要时间1就可以结束。
当有近道(i,j)时,可以直接跳过去。d(i)=(j)。
下面是代码:
问从0到n所花费时间平均时间。每次有投骰子,投到几就走几步。当然了,还有近道。
题目分析:
假设现在在i,那么接下来有六种可能的走法,分别是:
i到i+1,在由i+1到结束
i到i+2,在由i+2到结束
i到i+3,在由i+3到结束
i到i+4,在由i+4到结束
i到i+5,在由i+5到结束
i到i+6,在由i+6到结束
其中每一个可能的走法发生的概率为n为1/6。那么不妨定义d(i),表示从i走到结束的期望。
那么有下面的等式:
d(i-1) = sum((d(i+j)+1)*p) 其中j ∈[0,6]。
当i+j >= n时,只需要时间1就可以结束。
当有近道(i,j)时,可以直接跳过去。d(i)=(j)。
下面是代码:
#include <stdio.h> #include <string.h> #define maxn 101000 int h[maxn]; double d[maxn]; int N,M; void solve() { memset(d,0,sizeof(d)); for(int i = N; i >= 1; i--) { double p = 1.0/6.0; for(int j = 0; j < 6; j++) { int id = h[i-1]; if(id == -1) { if(i + j >= N) d[i-1] += p; else d[i-1] += p*(d[i+j]+1); } else { d[i-1] = d[id]; } } } } int main() { while(scanf("%d%d",&N,&M) && (N || M)) { memset(h,-1,sizeof(h)); for(int i = 0; i < M; i++) { int x,y; scanf("%d%d",&x,&y); h[x] = y; } solve(); printf("%.4lf\n",d[0]); } return 0; }
相关文章推荐
- 简单概率dp(期望)-hdu-4405-Aeroplane chess
- hdu 4405 Aeroplane chess(简单概率dp 求期望)
- hdu 4405 Aeroplane chess 概率dp
- [ACM] hdu 4405 Aeroplane chess (概率DP)
- hdu 4405 Aeroplane chess(概率dp)
- 【期望dp】hdu 4405 Aeroplane chess
- hdu 4405 Aeroplane chess (概率DP)
- HDU 4405 Aeroplane chess 概率dp
- HDU 4405:Aeroplane chess(概率DP)
- HDU 4405 Aeroplane chess (概率dp)
- hdu 4405 Aeroplane chess(概率+dp)
- HDU 4405 Aeroplane chess(概率DP入门)
- HDU 4405 Aeroplane chess(期望dp)
- HDU 4405 Aeroplane chess (概率DP)
- HDU 4405 Aeroplane chess 概率DP 难度:0
- hdu 4405 Aeroplane chess (概率DP+求期望)
- hdu 4405 Aeroplane chess(概率dp)
- HDU 4405 Aeroplane chess 概率dp
- HDU 4405 Aeroplane chess (概率DP求期望)
- HDU 4405 Aeroplane chess(期望DP)