hdu 3853 LOOPS 期望dp 分母不能为0

来源:互联网 发布:淘宝美工工作描述 编辑:程序博客网 时间:2024/04/30 22:02

LOOPS

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 3978    Accepted Submission(s): 1587


Problem Description
Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl).

Homura wants to help her friend Madoka save the world. But because of the plot of the Boss Incubator, she is trapped in a labyrinth called LOOPS.

The planform of the LOOPS is a rectangle of R*C grids. There is a portal in each grid except the exit grid. It costs Homura 2 magic power to use a portal once. The portal in a grid G(r, c) will send Homura to the grid below G (grid(r+1, c)), the grid on the right of G (grid(r, c+1)), or even G itself at respective probability (How evil the Boss Incubator is)!
At the beginning Homura is in the top left corner of the LOOPS ((1, 1)), and the exit of the labyrinth is in the bottom right corner ((R, C)). Given the probability of transmissions of each portal, your task is help poor Homura calculate the EXPECT magic power she need to escape from the LOOPS.




 

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 EOF


 

Output
A real number at 3 decimal places (round to), representing the expect magic power Homura need to escape from the LOOPS.

 

Sample Input
2 20.00 0.50 0.50 0.50 0.00 0.500.50 0.50 0.00 1.00 0.00 0.00
 

Sample Output
6.000
 

Source
2011 Invitational Contest Host by BUPT
 

Recommend
chenyongfu   |   We have carefully selected several similar problems for you:  3857 3854 3849 3850 3851 
 

Statistic | Submit | Discuss | Note



题意:给出一个n*m的迷宫,要求从(1,1)走到(n,m)点,求消耗魔法的期望。

    对于任意一个位置,移动一次需要消耗两点魔法,有三种情况:移动到当前格,右移一格,下移一格,题目

给出在每一个位置分别发生这三种情况的概率,保证和为1,且不会有越界的可能。



代码:2016/11/07

#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])#define mem(a,x)  memset(a,x,sizeof a)#define ysk(x)  (1<<(x))typedef long long ll;typedef pair<int, int> pii;const double inf =1e15;const double eps =1e-10;const int maxn= 1000+20   ;int n,m;struct Node{    double there,ri,down;}a[maxn][maxn];double dp[maxn][maxn];int main(){   while(~scanf("%d%d",&n,&m))    {        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                scanf("%lf%lf%lf",&a[i][j].there ,&a[i][j].ri,&a[i][j].down);            }        }        dp[n][m]=0;       for(int y=n;y>=1;y--)       {           for(int x=m;x>=1;x--)           {               if(y==n&&x==m)  continue;               if( fabs(a[y][x].there-1)<eps  ) {dp[y][x]=inf;continue;}               dp[y][x]= (2+a[y][x].ri*dp[y][x+1] +a[y][x].down*dp[y+1][x])/(1-a[y][x].there);           }       }       printf("%.3f\n",dp[1][1]);    }    return 0;}



原来的:


据说浮点数如果是分母,而且为0,结果会是NAN。我实测结果是inf。


#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<cctype>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI (4.0*atan(1.0))#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   ind<<1,le,mid#define rson    ind<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mk    make_pair#define _f     first#define _s     secondusing namespace std;//const int INF=    ;typedef long long ll;//const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);const int INF =0x3f3f3f3f;const int maxn= 1000+20   ;//const int maxm=    ;int n,m;struct Node{    double there,ri,down;}a[maxn][maxn];double dp[maxn][maxn];int main(){   while(~scanf("%d%d",&n,&m))    {        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                scanf("%lf%lf%lf",&a[i][j].there ,&a[i][j].ri,&a[i][j].down);            }        }        dp[n][m]=0;       for(int y=n;y>=1;y--)       {           for(int x=m;x>=1;x--)           {               if(y==n&&x==m)  continue;               if( fabs(a[y][x].there-1)<eps  ) {dp[y][x]=0;continue;}//一直wa,当初想到过分母为0的情况,               //可是不知道此时dp[y][x]应该=多少。实际上可以赋值为任何值都不会wa。               //我想了想,还是赋值为0好,因为不管花费多少魔法值,成功概率都是0,故期望为0。               dp[y][x]= (2+a[y][x].ri*dp[y][x+1] +a[y][x].down*dp[y+1][x])/(1-a[y][x].there);           }       }       printf("%.3f\n",dp[1][1]);    }    return 0;}


0 0
原创粉丝点击