HDU 4405 expectations

Turn to: https: //www.cnblogs.com/kevinACMer/p/3724671.html

Meaning of the questions:

There are a number line N + 1 points (numbered 0 ~ N), a person playing the game, starting from 0, N or reaches the point when N is greater than the end of the game. Every action a dice, dice numbered 1-6 to throw how much you walk a few steps forward, this number line and some special points, which is similar to the flight point in the flight chess, once they reach these points you can fly directly to given point. Seeking a total investment expected number of dice.

analysis:

By dp [i] i said in location, from the end of the game also threw desired number of times. Obviously dp [n] is 0, the request is dp [0]. For fly directly over the point. VIS e.g. an array [] is represented, vis [a] = b, indicates when it reaches a point may fly directly to the point b, then obviously dp [vis [a]] = dp [a]. Pushed backwards, DP I below 6 a possible state (i.e., corresponding to the six possible cube), each of which is 1/6 of the probability. Therefore, for (int x = 1; x <= 6; x ++) dp [i] + = dp [i + x] /6.0;dp [i] + = 1; note that after the last addition to the desired play Each possibility +1, because these six possibilities add up to just expect the next state, the current state of their previous state, it is desirable (understood as direct investment and the number of dice) to +1.

Code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <map>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long ll;
double dp[100020];
int vis[100020];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m) && (n+m)!=0)
    {
        memset(vis,-1,sizeof(vis));
        memset(dp,0,sizeof(dp));
        int a,b;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            vis[a]=b;
        }
        for(int i=n-1;i>=0;i--)
        {
            if(vis[i]==-1)
            {
                for(int j=1;j<=6;j++)
                {
                    cout<<dp[i+j]/6.0<<endl;
                    dp[i]+=dp[i+j]/6.0;
                    cout<<'i'<<" "<<dp[i]<<endl;
                }
                dp[i]+=1;
                cout<<i<<"   "<<dp[i]<<endl;
            }
            else dp[i]=dp[vis[i]];
        }
        printf("%.4lf\n",dp[0]);
    }
    return 0;
}

Published 43 original articles · won praise 0 · Views 1064

Guess you like

Origin blog.csdn.net/weixin_45719073/article/details/104505609