hdu 4487 Maximum Random Walk (概率dp)

Maximum Random Walk

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 219    Accepted Submission(s): 120


Problem Description
Consider the classic random walk: at each step, you have a 1/2 chance of taking a step to the left and a 1/2 chance of taking a step to the right. Your expected position after a period of time is zero; that is, the average over many such random walks is that you end up where you started. A more interesting question is what is the expected rightmost position you will attain during the walk.
 

Input
The first line of input contains a single integer P, (1 <= P <= 15), which is the number of data sets that follow. Each data set should be processed identically and independently.

Each data set consists of a single line of input consisting of four space-separated values. The first value is an integer K, which is the data set number. Next is an integer n, which is the number of steps to take (1 <= n <= 100). The final two are double precision floating-point values L and R
which are the probabilities of taking a step left or right respectively at each step (0 <= L <= 1, 0 <= R <= 1, 0 <= L+R <= 1). Note: the probably of not taking a step would be 1-L-R.
 

Output
For each data set there is a single line of output. It contains the data set number, followed by a single space which is then followed by the expected (average) rightmost position you will obtain during the walk, as a double precision floating point value to four decimal places.
 

Sample Input
  
  
3 1 1 0.5 0.5 2 4 0.5 0.5 3 10 0.5 0.4
 

Sample Output
  
  
1 0.5000 2 1.1875 3 1.4965
 

Source

思路:
用三维数组来表示状态:dp[i][j][k]  i-走了i步 j-到达第j个位置 k-右移最大距离为k,递推完后求期望。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 201
using namespace std;

int n,m;
float pl,pr,pm,ans;
float dp[101][maxn][maxn];

int main()
{
    int i,j,k,t,test,l,r;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%f%f",&test,&n,&pl,&pr);
        pm=1-pl-pr;
        memset(dp,0,sizeof(dp));
        dp[0][100][100]=1;            // 将一百设为起点
        l=100-n;
        r=100+n;
        for(i=1;i<=n;i++)             // 走了i步
        {
            for(j=l;j<=r;j++)         // 到达第j个位置
            {
                for(k=100;k<=r;k++)   // 最大到达的右边界
                {
                    dp[i][j][k]+=dp[i-1][j][k]*pm;
                    dp[i][j-1][k]+=dp[i-1][j][k]*pl;
                    if(j+1>k) dp[i][j+1][j+1]+=dp[i-1][j][k]*pr;
                    else dp[i][j+1][k]+=dp[i-1][j][k]*pr;
                }
            }
        }
        ans=0;
        for(k=100;k<=r;k++)
        {
            for(j=l;j<=r;j++)
            {
                ans+=dp[n][j][k]*(k-100);
            }
        }
        printf("%d %.4f\n",test,ans);
    }
    return 0;
}





 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值