本文已参与「新人创作礼」活动,一起开启掘金创作之路。
数据:
Input
The first line contains two integers R and C (2 <= R, C <= 1000).
The following R lines, each contains C*3 real numbers, at 2 decimal places. Every three numbers make a group. The first, second and third number of the cth group of line r represent the probability of transportation to grid (r, c), grid (r, c+1), grid (r+1, c) of the portal in grid (r, c) respectively. Two groups of numbers are separated by 4 spaces.
It is ensured that the sum of three numbers in each group is 1, and the second numbers of the rightmost groups are 0 (as there are no grids on the right of them) while the third numbers of the downmost groups are 0 (as there are no grids below them).
You may ignore the last three numbers of the input data. They are printed just for looking neat.
The answer is ensured no greater than 1000000.
Terminal at EOFOutput
A real number at 3 decimal places (round to), representing the expect magic power Homura need to escape from the LOOPS.
Sample Input
2 2 0.00 0.50 0.50 0.50 0.00 0.50 0.50 0.50 0.00 1.00 0.00 0.00Sample Output
6.000
题意: R*C的矩阵,起点在(1,1),终点在(R,C)
然后是每个点在原地,向右,向下的概率,每次花费2个能量去移动(呆在原地也可),求到达终点花费能量的期望值
注意: 题目说期望小于1e6,那么呆在那个点的概率为1的点肯定从起点出发不可达的点。否则期望会无穷大
dp[i][j]表示从(i,j)到(r,c)所需要的期望能量。
code:
#include<stdio.h>
#include<string.h>
#include<math.h>/// fabs函数
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int N=1e3+10;
double dp[N][N],a[N][N][3];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
for(int k=0; k<3; k++)
scanf("%lf",&a[i][j][k]);
mem(dp,0);
for(int i=n; i>=1; i--)
for(int j=m; j>=1; j--)
{
if(fabs(1-a[i][j][0])<1e-7)
continue;///停留在原点的概率为1
dp[i][j]=(dp[i][j+1]*a[i][j][1]+dp[i+1][j]*a[i][j][2]+2)/(1.0-a[i][j][0]);
}
printf("%.3f\n",dp[1][1]);
}
return 0;
}