HDU 3853(期望DP)

题意:

在一个r*c的网格中行走,在每个点分别有概率向右、向下或停止不动。每一步需要的时间为2,问从左上角走到右下角的期望时间。

SOL:

非常水一个DP...(先贴个代码挖个坑

code:

/*==========================================================================
# Last modified: 2016-01-20 23:08
# Filename: HDU3853.cpp
# Description: 
==========================================================================*/
#define me AcrossTheSky 
#include <cstdio> 
#include <cmath> 
#include <ctime> 
#include <string> 
#include <cstring> 
#include <cstdlib> 
#include <iostream> 
#include <algorithm> 
  
#include <set> 
#include <map> 
#include <stack> 
#include <queue> 
#include <vector> 
#define lowbit(x) (x)&(-x) 
#define INF 1070000000 
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++) 
#define FORP(i,a,b) for (int i=(a);i<=(b);i++) 
#define FORM(i,a,b) for (int i=(a);i>=(b);i--) 
#define ls(a,b) (((a)+(b)) << 1) 
#define rs(a,b) (((a)+(b)) >> 1) 
using namespace std; 
typedef long long ll; 
typedef unsigned long long ull; 
/*==================split line==================*/ 
struct P{
	double on,r,d;
}prob[1050][1050];
double dp[1050][1050];

int main(){ 
	int r,c;
    while (scanf("%d%d",&r,&c)!=EOF){ 
	
	memset(prob,0,sizeof(prob));
	memset(dp,0,sizeof(dp));
	for (int i=1;i<=r;i++) 
		for (int j=1;j<=c;j++) {
			scanf("%lf%lf%lf",&prob[i][j].on,&prob[i][j].r,&prob[i][j].d);
			//dp[i][j]=prob[i][j].on*2.0;
	}
	for (int i=r;i>=1;i--)
		for (int j=c;j>=1;j--)
			if (i==r && j==c) continue;
			else if (prob[i][j].on-1==0) dp[i][j]=0;
			else{
				dp[i][j]=(dp[i+1][j]*prob[i][j].d+dp[i][j+1]*prob[i][j].r+2)/(1-prob[i][j].on);
		}
	for (int i=1;i<=r;i++){
		for (int j=1;j<=c;j++) printf("%.3lf ",dp[i][j]);
		cout << endl;
		}
		printf("%.3lf
",dp[1][1]);
	}
}


Sometimes it s the very people who no one imagines anything of. who do the things that no one can imagine.
原文地址:https://www.cnblogs.com/YCuangWhen/p/5188090.html