The number of steps Time Limit: 1000 ms memory limit: 65536 k any questions? Click Here ^_^ Description
Mary stands in a strange maze, the maze looks like a triangle (the first layer have one room, the second layer have two rooms, the third layer have three rooms ...). Now she stands at the top point (the first layer), and the key of this maze is in the lowest layer's leftmost room. known That each room can only access to its left room and lower left and lower right rooms. if a room doesn' t have its left room, the probability of going to the lower left room and lower right room are a and B (a + B = 1 ). if a room only has it's left room, the probability of going To the room is 1. if a room has its lower left, lower right rooms and Its left room, the probability of going to each room are C, D, E (C + D + E = 1 ). now, Mary wants to know how many steps she needs to reach the key. dear friend, can you tell Mary the expected number of steps required to reach the key?
Enter There are no more than 70 test cases. in each case, first input a positive integer N (0the input is terminated with 0. this test case is not to be processed. output Please calculate the expected number of steps required to reach the key room, there are 2 digits after the decimal point. sample Input
30.3 0.70.1 0.3 0.60
Sample output
3.41
Tip: the first question of the probability DP in the fourth ACM College Student Program Design Competition in Shandong province in 2013 is simple. The expectation from the last vertex to the last vertex is 0, and the expectation from the vertex connected by the other vertex is obtained. If the probability of I to J is PIJ, the probability of I to I is PII, and the expectation is E, the expectation of 1 to 4 is 1. e4 = 0; 2. e3 = E3 * p33 + E4 * p34; 3. e2 = e2 * P22 + E4 * p24; 4. e1 = e1 * P11 + e2 * p12 + E3 * P13; memory-based search, final release of required values
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;double dp[100][100] ;double a , b , c , d , e ;int i , j , n ;int ff(int x,int y){ if( x <= n && y >=(n+1)-x ) return 1 ; return 0 ;}void f(){ return ;}int main(){ while(scanf("%d", &n) && n) { scanf("%lf %lf", &a, &b); scanf("%lf %lf %lf", &c, &d, &e); memset(dp,0,sizeof(dp)); for(i = n ; i >= 1 ; i--) { for(j = (n+1)-i ; j <= n ; j++) { if(i == n && j == (n+1)-i) continue ; else if( i == n ) dp[i][j] = 1.0*( dp[i][j-1] ) + 1.0 ; else { if( j == (n+1)-i ) dp[i][j] = a*dp[i+1][j-1] + b*dp[i+1][j] + 1.0 ; else dp[i][j] = c*dp[i+1][j-1] + d*dp[i+1][j] + e*dp[i][j-1] + 1.0 ; } } } printf("%.2lf\n", dp[1][n]); } return 0;}
Sdut2623 -- the number of steps (probability DP first play)