概率DP:飞行棋hdu4405

Description
Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are 1,2,3,4,5,6). When Hzz is at grid i and the dice number is x, he will moves to grid i+x. Hzz finishes the game when i+x is equal to or greater than N.

There are also M flight lines on the chess map. The i-th flight line can help Hzz fly from grid Xi to Yi (0

#include <cstdio>
#include <cstring>
using namespace std;
int n,m,t1,t2, fly[100009];
double dp[100009];
int main()
{
    while(scanf("%d%d",&n,&m)&&(n||m))
    {
        memset(fly,-1,sizeof fly);
        memset(dp,0,sizeof dp);
        while(m--)
        {
            int t2,t1;
            scanf("%d%d",&t1,&t2);
            fly[t1]=t2;
        }
        for(int i=n-1;i>=0;i--)
        {
            if(fly[i]!=-1)
                dp[i]=dp[fly[i]];
            else
            {
                for(int j=1;j<=6;j++)
                    dp[i]+=dp[i+j];
                    dp[i]=dp[i]/6+1;
            }
        }
        printf("%.4f\n",dp[0]);
    }
    return 0;
}

思路:
1,若不能直接飞行则从一个位置可以出发到六个不同的位置,概率为与到该位置之和,由于六种可能机会均等,所以结果要除以6并加上一(不管走到哪里都会头一次骰子;
2,若可以直接飞行则为飞到该位置的概率;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值