HDU 4405 Aeroplane chess 概率DP 难度:0

http://acm.hdu.edu.cn/showproblem.php?pid=4405

明显,有飞机的时候不需要考虑骰子,一定是乘飞机更优

设E[i]为分数为i时还需要走的步数期望,j为某个可能投出的点数如果从i向i-j推导,我们并不能确定i的转移方向,因为可能有两个i-j有飞机其目的地是i,所以我们选择从i向i+j推导期望

如果设G[i]为分数为i时已经走过的步数期望,那么要确定G[i+j]需要知道P(i|i+j),也即转移到i+j的条件下从i转移来的概率,比较麻烦

由题意,设match[i]为i处飞机的目的地,则当match[i]存在时,E[i]=E[match[i]],否则E[i]=sigma((E[i+j]+1)*(1/6)),j in range(6)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e5+5;
double e[maxn];
int match[maxn];
int n,m;
void init()
{
    memset(e,0,sizeof(e));
    memset(match,0,sizeof(match));
    for(int i=0;i<m;i++){
        int f,t;
        scanf("%d%d",&f,&t);
        match[f]=t;
    }
}
int lt(int a){return min(a,n);}
void calc()
{
    for(int i=n-1; i>=0; i--)
    {
        if(match[i]!=0)e[i]=e[lt(match[i])];
        else
        {
            for(int j=1; j<=6; j++)
            {
                e[i]+=(e[lt(i+j)]+1)/6;
            }
        }
    }
}
int main()
{
    for(int ti=1; scanf("%d%d",&n,&m)==2&&(n||m); ti++)
    {
        init();
        calc();
        printf("%.4f\n",e[0]);
    }

    return 0;
}

 

posted @ 2015-05-05 16:58  雪溯  阅读(124)  评论(0编辑  收藏  举报