【期望DP】 HDU 4405 Aeroplane chess

通道

题意:飞行棋,从0到n,置骰子,置到几就往前走几步,前进中会有捷径,比如2和5连到一起了,那你走到2时可以直接跳到5,最后问跳到n时平均置几次骰子

思路:dp[i]:到达i是,离终点的期望次数

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;
const int N=100005;

struct node
{
    int y,next;
};

bool vis[N];
node path[N];
int first[N],t;
double dp[N];

void add(int x,int y) {
    path[t].y=y;
    path[t].next=first[x];
    first[x]=t++;
}

int main() {
    double s;
    int n,m,v;
    while(cin>>n>>m) {
        if(m==0&&n==0) break;
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        memset(first,0,sizeof(first));
        int x,y;
        t=1;
        while(m--){
            cin>>x>>y;
            add(y,x);
        }
        dp[n]=-1;
        for(int i=n; i>=0; i--) {
            if(!vis[i]) {
                vis[i]=true;
                s=0;
                for(int k=1; k<=6; k++)
                    s+=dp[i+k];
                s/=6;
                dp[i]+=(s+1);
            }
            for(int k=first[i]; k; k=path[k].next) {
                v=path[k].y;
                dp[v]=dp[i];
                vis[v]=true;
            }
        }
        printf("%.4lf\n",dp[0]);
    }
    return 0;
}

时间: 2024-10-14 15:32:14

【期望DP】 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>

[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

题意: 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

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+求期望)

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入门题

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)

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

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&