HDU 3853 down to the right for an exit question-expected DP

Source: Internet
Author: User

Test instructions: The initial state at (the primary) position, the goal is to go (n,n), each can only downward to the right or not to move, known in each lattice when the probability of these three cases, each move one step consumes 2 of the magic, to go to the end of the use of the magic of the expectation.

Analysis: Simple expected DP, to apply the previous frame, but this question is not +1, but +2, because each time the number of extra is to take a step of consumption , here is 2! Note P1[i][j]==1 can not calculate dp[i][j], see the equation will know, the denominator can not be 0.

Code:

#include <iostream> #include <cstdio>using namespace Std;int n,m;double p1[1005][1005],p2[1005][1005],p3[ 1005][1005],dp[1005][1005];int Main () {while (scanf ("%d%d", &n,&m)!=eof) {for (Int. i=0;i<n;i++) for (int j=0; j<m;j++) scanf ("%lf%lf%lf", &p1[i][j],&p2[i][j],&p3[i][j]); Memset (Dp,0,sizeof (DP)); for (int i=n-1;i >=0;i--) {for (int j=m-1;j>=0;j--) {if (i==n-1&&j==m-1) continue;if (p1[i][j]==1.0) continue; dp[i][j]=p2 I [J]*dp[i][j+1]+p3[i][j]*dp[i+1][j]+2.0;dp[i][j]/= (1.0-p1[i][j]);}} printf ("%.3lf\n", Dp[0][0]);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3853 down to the right for an exit question-expected DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.