hdu 4405 Aeroplane chess 动态规划

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

所谓概率DP大多是算期望

算期望大多是从终点往回推

简单的1维动态规划

dp[i]表示第i格到终点的期望步数

从终点往前推

若当前为某条捷径的起点 那么该点的dp值直接等于捷径终点的dp值

否则枚举掷出1到6时所到的位置的期望 用最朴素的方法求期望

dp[0]即为所求

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <vector>

using namespace std;

const int maxn = 100010;

double dp[maxn];
int e[maxn];

int main()
{
    //freopen("in.txt", "r", stdin);

    int n, m;
    while(scanf("%d%d", &n, &m) == 2 && n != 0)
    {
        memset(e, -1, sizeof(e));
        memset(dp, 0, sizeof(dp));

        int u, v;
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d", &u, &v);
            e[u] = v;
        }

        for(int i = n-1; i >= 0; i--)
        {
            if(e[i] != -1)
                dp[i] = dp[e[i]];
            else
            {
                for(int j = 1; j <= 6; j++)
                {
                    dp[i] += (dp[i+j] + 1)/6;
                }
            }
        }

        printf("%.4f\n", dp[0]);
    }

    return 0;
}
时间: 2024-08-01 22:47:24

hdu 4405 Aeroplane chess 动态规划的相关文章

HDU 4405 Aeroplane chess (概率DP求期望)

题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点需要步数的期望 其中有m个跳跃a,b表示走到a点可以直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点可以到走到i+1,i+2,i+3,i+4,i+5,i+6 点且每个点的概率都为1/6 所以dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6  + 1(步数加一). 而对于有跳跃的点直接为dp[a]=dp[b]; #include<stdio.h>

hdu 4405 Aeroplane chess

题意: hzz一开始在0位置,然后hzz掷骰子,骰子为i,就往前走i步,当hzz位置大于等于n的时候结束,求掷骰子次数的期望 有m个直达点 (x,y),走到x时可以直接到y 求期望一般从后往前推 当 i不等于任何一个x时 dp[i]=seg(1/6*dp[i+k])+1 否则 dp[i]=dp[y] 1 #include<iostream> 2 #include<string> 3 #include<cstdio> 4 #include<vector> 5

[ACM] hdu 4405 Aeroplane chess (概率DP)

Aeroplane chess Problem 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 number

hdu 4405 Aeroplane chess(概率DP 求期望__附求期望讲解方法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 Problem 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 p

hdu 4405 Aeroplane chess 概率dp入门题

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

hdu 4405 Aeroplane chess (概率DP+求期望)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1551    Accepted Submission(s): 1063 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to

hdu 4405 Aeroplane chess (概率DP)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1503    Accepted Submission(s): 1025 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids la

hdu 4405 Aeroplane chess(概率+dp)

Problem 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 a

HDU 4405 Aeroplane chess 概率DP 水题

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2327    Accepted Submission(s): 1512 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids lab