首页 > 代码库 > sdut2623——The number of steps

sdut2623——The number of steps

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
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大学生程序设计竞赛

示例程序

 
  • 提交
  • 状态
  • 讨论

水题概率dp


/*************************************************************************
    > File Name: sdut2623.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2014年12月28日 星期日 15时45分25秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

double a, b, c, d, e;
double dp[1000][1000];
int n;

double dfs(int i, int j)
{
	if (i > n || i < 1 || j < 1 || j > i)
	{
		return 0;
	}
	if (dp[i][j] != -1)
	{
		return dp[i][j];
	}
	if (j <= 1) //没有左边的房间
	{
		dp[i][j] = a * dfs(i + 1, j) + b * dfs(i + 1, j + 1) + 1;
	}
	else
	{
		if (i == n)
		{
			dp[i][j] = dfs(i, j - 1) + 1;
		}
		else
		{
			dp[i][j] = e * dfs(i, j - 1) + c * dfs(i + 1, j) + d * dfs(i + 1, j + 1) + 1;
		}
	}
//	printf("dp[%d][%d] = %.2f\n", i, j, dp[i][j]);
	return dp[i][j];
}

int main()
{
	while (~scanf("%d", &n), n)
	{
		for (int i = 0; i <= n; ++i)
		{
			for (int j = 0; j <= n; ++j)
			{
				dp[i][j] = -1;
			}
		}
		dp[n][1] = 0;
		scanf("%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e);
		printf("%.2f\n", dfs(1, 1));
	}
	return 0;
}


sdut2623——The number of steps