Question connection: Ultraviolet A 10529-dumb bones
Given N, it indicates that N dominoes are to be put. Each time you put down the dominoes, the probability of descending to the left is PL, and the probability of descending to the right is PR. If you fall down, all the dominoes on that side will be pushed down. You can choose to place the dominoes one by one and ask what is the expected minimum number of dominoes.
Solution: DP [I] indicates the expected number of steps required to put one card, maintaining an optimal position, DP [I] = min \ {(steps from I-1 to I)} + (Steps 0 to i-1 )}
- (Steps from I-1 to I): DP [I? J? 1]? PL + dp [J]? PR + 11? Pl? PR
- (Steps from 0 to i-1): DP [J] + dp [I? J? 1]
So: DP [I] = min {DP [I? J? 1]? PL + dp [J]? PR + 11? Pl? PR + dp [J] + dp [I? J? 1]}
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int maxn = 1005;const double INF = 0x3f3f3f3f3f3f3f;int N;double pl, pr, dp[maxn];double solve () { double ac = 1 - pl - pr; for (int i = 1; i <= N; i++) { dp[i] = INF; for (int j = 0; j < i; j++) { double tmp = dp[i-j-1] * pl + dp[j] * pr; tmp = dp[j] + dp[i-j-1] + (tmp + 1) / ac; if (tmp < dp[i]) dp[i] = tmp; } } return dp[N];}int main () { while (scanf("%d", &N) == 1 && N) { scanf("%lf%lf", &pl, &pr); printf("%.2lf\n", solve()); } return 0;}