题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=3853
题意
nm网格,每个点可以花费2进行移动,移动是右,下,原地不动,这三者都有其概率,问1-1到n-m期望步数
思路
题目似乎有BUG,不过无所谓了
期望DP入门题,DP[i][j]为ij处到终点期望步数,显然DP[n][m]=0,DP[1][1]为答案,易写出方程
dp[i][j]=(2+dp[i][j+1]*p2[i][j]+dp[i+1][j]*p3[i][j])/(1.0-p1[i][j]);
教训/收获
期望DP常见套路,将dp状态存储的值定义为到达终态的期望,最终求初始状态期望。需要从终态倒推
代码
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<chrono>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
//#define int long long
//#define double long double
using namespace std;
typedef long long ll;
const int maxn=1005;
const int inf=0x3f3f3f3f;
int n,m,k;
double dp[maxn][maxn];
double p1[maxn][maxn],p2[maxn][maxn],p3[maxn][maxn];
signed main(){
#ifndef ONLINE_JUDGE
freopen("IO\\in.txt","r",stdin);
freopen("IO\\out.txt","w",stdout);
#endif
int tn=1;
while(~scanf("%d%d",&n,&m)){
memset(dp,0,sizeof dp);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
scanf("%lf%lf%lf",&p1[i][j],&p2[i][j],&p3[i][j]);
}
for(int i=n;i;i--)
for(int j=m;j;j--){
if(i==n&&j==m) continue;
if(p1[i][j]==1.0) continue;
dp[i][j]=(2+dp[i][j+1]*p2[i][j]+dp[i+1][j]*p3[i][j])/(1.0-p1[i][j]);
}
printf("%.3lf\n",dp[1][1]);
}
}