码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3640 概率dp

时间:2015-07-29 15:58:57      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:acm   zoj   概率dp   

Cain 被困在一个迷宫里面,迷宫有N个出口,Cain 每天可以随机选择一个出口,每个出口有一些怪兽,Cain 需要c[i]的武力值才能花 t[i] 天通过。否则 Cain 不能通过,并且他

的武力值会增加c[i]。
给出N条路的 c[i]、t[i] 还有 Cain 初始的武力值,求Cain 离开迷宫所需要的天数的期望。
由题目可以发现,应抓住武力值的变化。
dp[i] : 武力值在i的情况下出去所需天数的期望。Cain 可能选到可以出去的路,也可能选到出不去的路。
dp[i] = (Σt[j] + Σ(1 + dp[i+c[k]]))/n;  (c[j] < i, c[k] >= i)

因为该条路出不去的话武力值会成长c[i],所以不存在出不去的情况。

tips:用递归的写法更加简单,易懂。


/***********************************************
 ** problem ID	: zoj_3640.cpp
 ** create time	: Thu Jul 23 20:57:26 2015
 ** auther name	: xuelanghu
 ** auther blog	: blog.csdn.net/xuelanghu407
 **********************************************/

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int n, f;
int c[110];

int t(int c) {
	return (int ) ((1 + sqrt(5)) / 2 * c * c);
}

double DP (int f) {
	double res = 0.0;
	for (int i=0; i<n; i++) {
		if (f > c[i]) {
			res += t(c[i]);
		} else {
			res += (1 + DP(f + c[i]));
		}
	}
	return res / n;
}

int main () {
	while (cin >> n >> f) {
		for (int i=0; i<n; i++) {
			cin >> c[i];
		}
		printf ("%.3lf\n", DP(f));
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

zoj 3640 概率dp

标签:acm   zoj   概率dp   

原文地址:http://blog.csdn.net/xuelanghu407/article/details/47128195

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!