HDU - 3853(简单概率dp , 注意边界)

本题目直接按题目意思推方程转移就行了,但是有个坑点,就是对于一个点i,j从1,1开始不可达,那么该点的p1(转移到自己)就有可能为1,所以当转移到一个点的概率为0的时候

那么就不往该点转移。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <list>
#include <cstdlib>
#include <queue>
#include <stack>
#include <cmath>
#define ALL(a) a.begin(), a.end()
#define clr(a, x) memset(a, x, sizeof a)
#define fst first
#define snd second
#define pb push_back
#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rep1(i,x,y) for(int i=x;i<=y;i++)
#define rep(i,n) for(int i=0;i<(int)n;i++)
using namespace std;
const double eps = 1e-10;
typedef long long LL;
typedef long long ll;
typedef pair<int, int> pii;
const int inf =0x3f3f3f3f;

const int N = 1010;
double p[N][N][4],E[N][N];
bool vis[N][N];
int n,m;
double dp(int i,int j){
   if(vis[i][j]) return E[i][j];
   vis[i][j] = 1;
   if(i == n && j == m) return E[i][j] = 0;
   E[i][j]=2;
   if(j < m && p[i][j][2] > eps) E[i][j]+=dp(i,j+1)*p[i][j][2];
   if(i < n && p[i][j][3] > eps) E[i][j]+=dp(i+1,j)*p[i][j][3];
   E[i][j]/=(1-p[i][j][1]);
   return E[i][j];
}
int main()
{
   while(scanf("%d %d",&n,&m)==2){
      rep1(i,1,n) rep1(j,1,m)
        scanf("%lf %lf %lf",&p[i][j][1],&p[i][j][2],&p[i][j][3]);
      memset(vis,false,sizeof(vis));
      printf("%.3lf\n",dp(1,1));
   }
   return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值