hdu4405 (期望)http://kicd.blog.163.com/blog/static/126961911200910168335852/

时间:2023-02-06 00:41:19


期望的学习:http://kicd.blog.163.com/blog/static/126961911200910168335852/

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405

思路:dp[i]表示从第i步到第n步投掷次数的期望,如果没有特殊的途径,dp[i]=(dp[i+1]+dp[i+2]+...+dp[i+5]+dp[i+6])/6+1;

#include<cstdio>
#include<cstring>
using namespace std;
const int num=100005;
int n,m,p[num];
double dp[num];
int main()
{
    int i,j,a,b;
    double ans;
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&n==0)
            break;
        memset(p,-1,sizeof(p));
        memset(dp,0,sizeof(dp));
        while(m--)
        {
            scanf("%d%d",&a,&b);
            p[a]=b;
        }
        for(i=n-1;i>=0;i--)
        {
            if(p[i]!=-1)
                dp[i]=dp[p[i]];
            else
            {
                for(j=1;j<=6;j++)
                    dp[i]+=dp[i+j];
                dp[i]=dp[i]/6.0+1;
            }
        }
        printf("%.4lf\n",dp[0]);
    }
    return 0;
}