首页 > 代码库 > HDU 3853 LOOPS

HDU 3853 LOOPS

期望,$dp$。

设$dp[i][j]$为位置$i$,$j$到达终点$R$,$C$的期望花费。

那么,$dp[i][j]=(p[i][j][1]*dp[i][j+1]+p[i][j][2]*dp[i+1][j]+2)/(1-p[i][j][0])$。

有一个坑点就是:如果某一格只能走到自己,那么这一格的$dp[i][j]$直接设为$0$。

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<iostream>using namespace std;typedef long long LL;const double pi=acos(-1.0),eps=1e-6;void File(){    freopen("D:\\in.txt","r",stdin);    freopen("D:\\out.txt","w",stdout);}template <class T>inline void read(T &x){    char c = getchar();    x = 0;    while(!isdigit(c)) c = getchar();    while(isdigit(c))    {        x = x * 10 + c - 0;        c = getchar();    }}int R,C;double dp[1005][1005];double p[1005][1005][3];int main(){    while(~scanf("%d%d",&R,&C))    {        for(int i=1;i<=R;i++)            for(int j=1;j<=C;j++)                for(int k=0;k<=2;k++)                    scanf("%lf",&p[i][j][k]);        memset(dp,0,sizeof dp);        for(int i=R;i>=1;i--)        {            for(int j=C;j>=1;j--)            {                if(i==R&&j==C) continue;                if(p[i][j][0]==1.0) {dp[i][j]=0; continue;}                dp[i][j]=(p[i][j][1]*dp[i][j+1]+p[i][j][2]*dp[i+1][j]+2)/(1-p[i][j][0]);            }        }        printf("%.3f\n",dp[1][1]);    }    return 0;}

 

HDU 3853 LOOPS