概率DP求解例题
1,逆推状态:山东省赛2013年I题
Problem I: The number of steps
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
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
思路:二维DP,d[i][j]表示在i行第j个的期望,
然后逆推,点有三种情况:
1)只能靠左走(最后一行):d[i][j] = 1;
2)能靠左走,能靠下一行的左,右走:d[i][j] = d[i][j-1]*c+d[i+1][j-1]*d + d[i+1][j]*e;
3)只能向下一层的左右走:d[i][j] = d[i+1[j-1]*a+d[i+1][j]*b;
1 #include <bits/stdc++.h> 2 #define repu(i,a,b) for(int i=a;i<b;i++) 3 using namespace std; 4 #define N 50 5 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 int n; 10 while(cin>>n&&n) 11 { 12 double dp[N][N]; 13 double a,b,c,d,e; 14 cin>>a>>b>>c>>d>>e; 15 memset(dp,0.00,sizeof(dp)); 16 for(int i=n; i; i--)///µÚiÐÐ 17 { 18 for(int j = n+1-i; j<=n; j++)///µÚj¸ö 19 { 20 if(i==n&&j==(n+1-i)) 21 continue; 22 else if(i==n) 23 dp[i][j] = j -1; 24 else if(j==(n+1-i)) 25 dp[i][j] = dp[i+1][j-1]*a+dp[i+1][j]*b+1; 26 else 27 dp[i][j] = dp[i+1][j-1]*c+dp[i+1][j]*d+dp[i][j-1]*e+1; 28 } 29 } 30 cout<<fixed<<setprecision(2)<<dp[1][n]<<endl; 31 } 32 return 0; 33 }
二,先算概率,后算期望
Description
ICPC (International Cardboard Producing Company) is in the business of producing cardboard boxes. Recently the company organized a contest for kids for the best design of a cardboard box and selected M winners. There are N prizes for the winners, each one carefully packed in a cardboard box (made by the ICPC, of course). The awarding process will be as follows:
- All the boxes with prizes will be stored in a separate room.
- The winners will enter the room, one at a time.
- Each winner selects one of the boxes.
- The selected box is opened by a representative of the organizing committee.
- If the box contains a prize, the winner takes it.
- If the box is empty (because the same box has already been selected by one or more previous winners), the winner will instead get a certificate printed on a sheet of excellent cardboard (made by ICPC, of course).
- Whether there is a prize or not, the box is re-sealed and returned to the room.
Input
).
Output
Sample Input
sample input |
sample output |
5 7 |
3.951424 |
sample input |
sample output |
4 3 |
2.3125 |
思路:一维DP,d[i]表示第i个人拿到奖品的概率;
从第二个人开始往后推,d[i] = d[i-1]*d[i-1] + (1-d[i-1])*(d[i-1]-1/n);
前一个人拿到奖品 + 前一个人没有拿到奖品
最后for循环累加d[i];d[i] 虽然是概率,但是在计算的时候已经把它那一步的期望算进去了,最后累加起来其实就是期望了。
1 /* 2 SGU 495 3 */ 4 #include<stdio.h> 5 #include<iostream> 6 #include<string.h> 7 #include<algorithm> 8 #include<math.h> 9 using namespace std; 10 const int MAXN=100010; 11 double dp[MAXN];//dp[i]表示第i个人得到礼物的概率 12 13 int main() 14 { 15 int n,m; 16 while(scanf("%d%d",&n,&m)!=EOF) 17 { 18 dp[1]=1; 19 for(int i=2;i<=m;i++) 20 {//第i个人得到礼物的概率:假如第i-1个人没有得到礼物,那么i得到礼物的概率和i-1一样。 21 //假如第i-1个人得到了礼物,那么i得到礼物的概率是i-1得到礼物概率减去1/n 22 dp[i]=(1-dp[i-1])*dp[i-1]+dp[i-1]*(dp[i-1]-1.0/n); 23 } 24 double ans=0; 25 for(int i=1;i<=m;i++)ans+=dp[i]; 26 printf("%.10lf\n",ans); 27 } 28 return 0; 29 }

浙公网安备 33010602011771号