SDUT - 2623 The number of steps(概率dp)

题目链接:点击查看

题目大意:给出一个 n 层的三角形,第一层有 1 个点,第二层有 2 个点,第三层有 3 个点 ... 第 n 层有 n 个点,现在规定从第一层的点向下出发:

  1. 如果左下方有点并且右下方有点,并且左侧没有点,那么从当前点到左下方的点的概率为 a ,到右下方的点的概率为 b
  2. 如果左下方有点且右下方有点且左侧有点,那么从当前点到左下方的点的概率为 c ,到右下方的点的概率为 d ,到左侧的点的概率为 e

问到达第 n 层的第一个点的期望步数

题目分析:概率 dp ,设 dp[ i ][ j ] 为第 i 层第 j 个数到达点 ( n , 1 ) 的期望步数,则显然第 n 层的可以直接得到,因为到达了第 n 层后,每次都只能向左边走,所以 dp[ n ][ i ] = dp[ n ][ i - 1 ] + 1,初始化 dp[ n ][ 1 ] = 0

再考虑转移状态,因为需要转移的两个状态都是相邻的,所以需要多加上一步再计算概率,具体实现参考代码吧

最后 dp[ 1 ][ 1 ] 就是答案了

代码:
 

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
using namespace std;
 
typedef long long LL;
 
typedef unsigned long long ull;
 
const int inf=0x3f3f3f3f;

const int N=50;

double dp[N][N];

int main()
{
#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);
	int n;
	while(scanf("%d",&n)!=EOF&&n)
	{
		double a,b,c,d,e;
		cin>>a>>b>>c>>d>>e;
		dp[n][1]=0;
		for(int i=2;i<=n;i++)
			dp[n][i]=dp[n][i-1]+1;
		for(int i=n-1;i>=1;i--)
		{
			dp[i][1]=(dp[i+1][1]+1)*a+(dp[i+1][2]+1)*b;
			for(int j=2;j<=i;j++)
				dp[i][j]=(dp[i+1][j]+1)*c+(dp[i+1][j+1]+1)*d+(dp[i][j-1]+1)*e;
		}
		printf("%.2f\n",dp[1][1]);
	}













	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/108453275