HDU 4487 Maximum Random Walk(概率DP)

题目链接:点击打开链接

思路:概率DP, 用d[i][j][k]表示第i步, 走到j点, 走过的最大值为k的概率。  然后最后用概率乘以最右边走到的点就是期望, 期望相加就是答案。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 200 + 10;
int T,n,m,id;
double d[2][maxn][maxn];
double a, b;
int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d%lf%lf",&id,&n,&a,&b);
        int m = n;
        printf("%d ",id);
        n *= 2;
        int u = 0;
        memset(d[u], 0, sizeof(d[u]));
        d[u][m][m] = 1.0;
        for(int i = 0; i < m; i++) {
            memset(d[u^1], 0, sizeof(d[u^1]));
            for(int j = 1; j< n; j++) {
                for(int k = 1; k < n; k++) {
                    d[u^1][j-1][k] += d[u][j][k] * a;
                    d[u^1][j+1][max(k, j+1)] += d[u][j][k] * b;
                    d[u^1][j][k] += d[u][j][k] * (1.0 - a - b);
                }
            }
            u ^= 1;
        }
        double ans = 0;
        for(int i = 0; i <= n; i++) {
            for(int j = 0; j <= n; j++) {
                ans += d[u][i][j] * (j - m);
            }
        }
        printf("%.4f\n",ans);
    }
    return 0;
}


你可能感兴趣的:(HDU,ACM-ICPC,概率DP)