题意:迷宫是一个R*C的布局,每个格子中给出停留在原地,往右走一个,往下走一格的概率,起点在(1,1),终点在(R,C),每走一格消耗两点能量,求出最后所需要的能量期望
#include<iostream> #include<cmath> #include<cstdio> #include<cstring> typedef long long ll; const int inf=0x3f3f3f3f; #define For(i,a,b) for(int i=a;i<=b;i++) #define FFor(i,a,b) for(int i=a;i>=b;i--) template <class T> inline T min(T a,T b,T c) { return min(min(a,b),c); } template <class T> inline T max(T a,T b,T c) { return max(max(a,b),c); } template <class T> inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d)); } template <class T> inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d)); } using namespace std; const int N=1e3+5; const double eps=1e-9; //***********name************** double f[N][N]; int R,C; double p0[N][N],p1[N][N],p2[N][N]; //***********function********** //****************************** int main() { // freopen("test.txt","r",stdin); while(~scanf("%d%d",&R,&C)){ memset(f,0,sizeof(f)); For(i,1,R) For(j,1,C) scanf("%lf%lf%lf",&p0[i][j],&p1[i][j+1],&p2[i+1][j]); f[R][C]=0; FFor(i,R,1) FFor(j,C,1) { if(i==R&&j==C)continue; if(fabs(p0[i][j]-1)<=eps)continue; f[i][j]=(f[i][j+1]*p1[i][j+1]+f[i+1][j]*p2[i+1][j]+2)/(1-p0[i][j]); } printf("%.3f ",f[1][1]); } return 0; }