转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
The number of steps
Time Limit: 1 Sec Memory Limit: 128 M
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大水题。。。
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
double dp[][];
int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n&&n){
double a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
CLR(dp,);
for(int i=n;i;i--){
for(int j = n+-i;j<=n;j++){
if(i==n&&j==(n+-i))continue;
else if(i==n)dp[i][j]=dp[i][j-]+1.0;
else if(j==(n+-i))dp[i][j]=a*dp[i+][j-]+b*dp[i+][j]+1.0;
else dp[i][j]=c*dp[i+][j-]+d*dp[i+][j]+e*dp[i][j-]+1.0;
}
}
cout<<fixed<<setprecision()<<dp[][n]<<endl;
}
return ;
}