The number of steps(概率dp)
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?
Input
There are no more than 70 test cases.
In each case , first Input a positive integer n(0<n<45), which means the layer of the maze, then Input five real number a, b, c, d, e. (0<=a,b,c,d,e<=1, a+b=1, c+d+e=1).
The 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
3 0.3 0.7 0.1 0.3 0.6 0
Sample Output
3.41
Hint
题解:
比如打靶打中8环的概率为0.3 ,打中7环的概率为0.7,那么打中环数的期望就是 8*0.3 + 7*0.7;
本题中我们用dp[i][j] 表示当前位置(i,j,表示房间的位置,最顶层的房间为(1,1),最低层最左边为(n,1))距离目的地还需要走的期望步数。那么目的地假设为dp[n][1] (根据建的坐标不一样,位置也不一样),那么dp[n][1]的值为0,因为已经到达目的地,不需要再走了。那么我们所求的就是dp[1][1] 开始的地方。所以解题的过程,就是一个逆推的过程。整个逆推过程完成,dp[1][1]内的值就是所求的期望步数。
代码:
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define is_lower(c) (c >= 'a' && c <= 'z') #define is_upper(c) (c >= 'A' && c <= 'Z') #define is_alpha(c) (is_lower(c) || is_upper(c)) #define is_digit(c) (c >= '0' && c <= '9') #define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define PI acos(-1) #define IO \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define For(i, a, b) for (int i = a; i <= b; i++) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; const ll inf = 0x3f3f3f3f; const double EPS = 1e-10; const ll inf_ll = (ll)1e18; const ll maxn = 100005LL; const ll mod = 1000000007LL; const int N = 50 + 5; double ans[N][N]; int main() { int n; while (cin >> n, n) { memset(ans, 0, sizeof(ans)); double a, b, c, d, e; cin >> a >> b >> c >> d >> e; For(i, 2, n) { ans[n][i] = ans[n][i - 1] + 1; } for (int i = n - 1; i >= 1; i--) { ans[i][1] = (ans[i + 1][1] + 1) * a + (ans[i + 1][2] + 1) * b; for (int j = 2; j <= i; j++) { ans[i][j] = (ans[i][j - 1] + 1) * e + (ans[i + 1][j] + 1) * c + (ans[i + 1][j + 1] + 1) * d; } } printf("%.2lf\n",ans[1][1]); } }