sdutoj 2623 The number of steps

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2623

 

The number of steps

 

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

    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?

 

输入

There are no more than 70 test cases. 
 
In each case , first Input a positive integer n(0<n<45),
The input is terminated with 0. This test case is not to be processed.

输出

Please calculate the expected number of steps required to reach the KEY room, there are 2 digits after the decimal point.

示例输入

3

0.3 0.7

0.1 0.3 0.6

0 

示例输出

3.41

提示

 

来源

2013年山东省第四届ACM大学生程序设计竞赛

示例程序

 

分析:

 

第一行有一个位置,第二行两个,第三行三个......第n行n个。此时你在最左上角的位置,如果你左面没有位置,只能往左下和右下走,概率(a,b)。否则可以往左,左下和右下三个方向走,,概率(c,d,e)。让你求到达最左下角的期望。

 

先科普一下数学期望吧:

首先,来看下期望有啥基本的公式。对离散型随机变量x,其概率为p,有

对随机变量A、B,有 

第二个式子表明了期望有线性的性质,简单理解就是期望之间可根据关系,简单运算(不严谨的理解)。 这就为我们解决一个期望问题,不断转化为解决另外的期望问题,最终转化到一个已知的期望上。

举一个求期望最简单的例子,见下图:

 

 

假设有个人在 1号节点处,每一分钟他会缘着边随机走到一个节点或者在原地停留,问他走到4号节点需要平均几分钟?

这是个简单的期望问题,我们用Ei(i=1,2,3,4) 表示从i号节点走到4号节点的数学期望值。根据题意对1号节点有

      E1=(1/3)*E1+(1/3)*E2+(1/3)*E3+1 ①

表示 他下一分钟可以走到2或者3或在原地1,每个可能概率是1/3 ,注意是下一分钟,故要加上1.

同理我们对节点2,3同样可以列出:

      E2=(1/3)*E1+(1/3)*E2+(1/3)*E4+1 ②

      E3=(1/3)*E1+(1/3)*E3+(1/3)*E4+1 ③

E4等于多少呢? 很明显

      E4=0 ④

因为他就是要到点4

这样上面1234式其实就是组成了一组方程组,解方程组就可得出E1!!,用高斯消元,复杂度是O(n^3)

从上述例子,我们可总结出如何解决期望类问题,根据题意,表示出各个状态的期望(上例的Ei,1234),根据概率公式,列出期望之间的方程,解方程即可。

 

 

AC代码:

 

 1 #include<stdio.h>

 2 #include<string.h>

 3 double dp[57][57];  4 int main()  5 {  6     int t;  7     while(scanf("%d",&t),t)  8  {  9         int i,j; 10         double a,b,c,d,e; 11         scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e); 12         memset(dp,0,sizeof(dp)); 13         dp[t][1]=0; 14         for(i=1;i<=t-1;i++) 15  { 16             dp[t][i+1]+=(dp[t][i]+1); 17  } 18         for(i=t-1;i>=1;i--) 19  { 20             dp[i][1]+=a*(dp[i+1][1]+1)+b*(dp[i+1][2]+1); 21             for(j=2;j<=i;j++) 22             dp[i][j]+=c*(dp[i+1][j]+1)+d*(dp[i+1][j+1]+1)+e*(dp[i][j-1]+1); 23  } 24         printf("%.2lf\n",dp[1][1]); 25  } 26     return 0; 27 }
View Code

 

 

官方标程:

 

 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <stdlib.h>

 4 #include <string>

 5 #include <iostream>

 6 #include <map>

 7 #include <vector>

 8 #include <algorithm>

 9 using namespace std; 10 #define MAXN 1000

11 #define eps 1e-8

12 double Atex[MAXN][MAXN]; 13 double a[2], b[3]; 14 int all; 15 inline int dcmp(double d) { 16     return d < -eps ? -1 : d > eps; 17 } 18 

19 void gauss(int n, int m) 20 { 21     int r,c,i,j; 22     for(r=c=0; r<n&&c<m; r++,c++) 23  { 24         for(i=r;i<n;i++) 25         if(dcmp(Atex[i][c])) break; 26         if(i==n)//{r--;continue;}

27             return; 28         if(i!=r) for(j=c;j<=m;j++) swap(Atex[i][j],Atex[r][j]); 29         for(i=r+1;i<n;i++) 30             if(Atex[i][c]!=0) 31  { 32                 double temp=Atex[i][c]/Atex[r][c]; 33                 for(j=c;j<=m;j++) 34                 Atex[i][j]-=Atex[r][j]*temp; 35  } 36  } 37     for(i=n-1;i>=0;i--) 38  { 39         Atex[i][m]/=Atex[i][i]; 40         Atex[i][i]=1; 41         for(j=i-1;j>=0;j--) Atex[j][m]-=Atex[i][m]*Atex[j][i]; 42  } 43     return; 44 } 45 

46 void makemap(int n) { 47     memset(Atex, 0, sizeof(Atex)); 48     all = (1+n)*n/2; 49     for (int i = 0; i < all; i ++) { 50         Atex[i][i] = 1; 51         Atex[i][all] = 1; 52  } 53     int t = 0, tt; 54     for (int i = 0; i < n-1; i ++) { 55         tt = t + i+1; 56         Atex[t][tt] = -1*a[0]; 57         Atex[t][tt+1] = -1*a[1]; 58         for (int j = t+1; j < tt; j ++) { 59             Atex[j][j+i+1] = -1*b[0]; 60             Atex[j][j+i+2] = -1*b[1]; 61             Atex[j][j-1] = -1*b[2]; 62  } 63         t = tt; 64  } 65     Atex[t][all] = 0; 66     for (int i = t+1; i < all; i ++) { 67         Atex[i][i-1] = -1; 68  } 69 } 70 

71 int main() 72 { 73     int n; 74     while(scanf("%d", &n) != EOF) { 75         if(n == 0) break; 76         for (int i = 0; i < 2; i ++) { 77             scanf("%lf", &a[i]); 78  } 79         for (int i = 0; i < 3; i ++) { 80             scanf("%lf", &b[i]); 81  } 82  makemap(n); 83  gauss(all, all); 84         printf("%.2f\n", Atex[0][all]); 85  } 86     return 0; 87 }
View Code

 

你可能感兴趣的:(number)