标签:dp
题目链接:http://acm.acmcoder.com/showproblem.php?pid=4405
题意:棋子,从0到n,置骰子,置到几就往前走几步,前进中会有捷径,比如2和5连到一起了,那你走到2时可以直接跳到5,如果5和8连到一起了,那你还可以继续跳到8,最后问跳到n时平均置几次骰子。也就是求期望。
解法:普通概率DP,dp[n] == 0 ;向前递推。
代码:
#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <fstream>
#include <set>
#include <map>
#include <math.h>
using namespace std;
const int MAXN = 100010;
int p[MAXN];
int m, n, x, y;
int ex[MAXN];
double dp[MAXN];
int main()
{
while (cin >> n >> m)
{
if (n == 0 && m == 0) break;
memset(dp,0,sizeof(dp));
memset(ex,-1,sizeof(ex));
for (int i = 1; i <= m; i++)
{
cin >> x >> y;
ex[x] = y;
}
for (int i = n - 1; i >= 0; i--)
{
if (ex[i] != -1) dp[i] = dp[ex[i]];
else
{
for (int j = 1; j <= 6; j++)
{
if (i + j >= n)
dp[i] += (1.0 / 6) * dp[n];
else
dp[i] += (1.0/6) * dp[i + j];
}
dp[i] += 1;
}
}
printf("%.4lf\n",dp[0]);
}
return 0;
}
hdu 4405 Aeroplane chess【概率DP求期望】
标签:dp
原文地址:http://blog.csdn.net/u014427196/article/details/46424577