hdu4405——Aeroplane chess

Aeroplane chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1760    Accepted Submission(s): 1181

Problem Description

Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are
1,2,3,4,5,6). When Hzz is at grid i and the dice number is x, he will moves to grid i+x. Hzz finishes the game when i+x is equal to or greater than N.

There are also M flight lines on the chess map. The i-th flight line can help Hzz fly from grid Xi to Yi (0<Xi<Yi<=N) without throwing the dice. If there is another flight line from Yi, Hzz can take the flight line continuously. It is granted that there is
no two or more flight lines start from the same grid.

Please help Hzz calculate the expected dice throwing times to finish the game.

Input

There are multiple test cases.

Each test case contains several lines.

The first line contains two integers N(1≤N≤100000) and M(0≤M≤1000).

Then M lines follow, each line contains two integers Xi,Yi(1≤Xi<Yi≤N).

The input end with N=0, M=0.

Output

For each test case in the input, you should output a line indicating the expected dice throwing times. Output should be rounded to 4 digits after decimal point.

Sample Input

2 0
8 3
2 4
4 5
7 8
0 0

Sample Output

1.1667
2.3441

Source

2012 ACM/ICPC Asia Regional Jinhua Online

Recommend

zhoujiaqi2010   |   We have carefully selected several similar problems for you:  5145 5144 5143 5142 5140

简单的概率dp题,考虑每一个位置i可以走到哪几个位置,如果当前位置没有飞行路线,那么dp[i] = (1/6)*dp[i + 1] + (1/6)*dp[i + 2] + (1/6)*dp[i + 3] + (1/6)*dp[i + 4] + (1/6)*dp[i + 5] + (1/6)*dp[i + 6] + 1

如果当前有路线,则dp[i] = dp[fly[i]](此时不用掷骰子)

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

using namespace std;

const int N = 100100;
double dp[N];
int n, m;
int fly[N];

int main()
{
	int x, y;
	while (~scanf("%d%d", &n, &m))
	{
		if (!n && !m)
		{
			break;
		}
		for (int i = 0; i <= n; ++i)
		{
			fly[i] = -1;
			dp[i] = 0;
		}
		for (int i = 0; i < m; ++i)
		{
			scanf("%d%d", &x, &y);
			fly[x] = y;
		}
		for (int i = n - 1; i >= 0; --i)
		{
			if (fly[i] != -1)
			{
				dp[i] = dp[fly[i]];
			}
			else
			{
				for (int j = 1; j <= 6; ++j)
				{
					if (i + j >= n)
					{
						dp[i] += (1.0 + dp[n]) / 6;
					}
					else
					{
						dp[i] += (1.0 + dp[i + j]) / 6;
					}
				}
			}
		}
		printf("%.4f\n", dp[0]);
	}
	return 0;
}
时间: 2024-11-05 19:28:07

hdu4405——Aeroplane chess的相关文章

[hdu4405]Aeroplane chess

题意:某人掷骰子,数轴上前进相应的步数,会有瞬移的情况,求从0到N所需要的期望投掷次数. 解题关键:期望dp的套路解法,一个状态可以转化为6个状态,则该状态的期望,可以由6个状态转化而来.再加上两个状态的消耗即可. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cmath> 6 #include<iost

HDU 4405 Aeroplane chess (概率DP求期望)

题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点需要步数的期望 其中有m个跳跃a,b表示走到a点可以直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点可以到走到i+1,i+2,i+3,i+4,i+5,i+6 点且每个点的概率都为1/6 所以dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6  + 1(步数加一). 而对于有跳跃的点直接为dp[a]=dp[b]; #include<stdio.h>

hdu 4405 Aeroplane chess

题意: hzz一开始在0位置,然后hzz掷骰子,骰子为i,就往前走i步,当hzz位置大于等于n的时候结束,求掷骰子次数的期望 有m个直达点 (x,y),走到x时可以直接到y 求期望一般从后往前推 当 i不等于任何一个x时 dp[i]=seg(1/6*dp[i+k])+1 否则 dp[i]=dp[y] 1 #include<iostream> 2 #include<string> 3 #include<cstdio> 4 #include<vector> 5

HDU 4405(Aeroplane chess)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2060    Accepted Submission(s): 1346 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids lab

hdu 4405 Aeroplane chess(概率DP 求期望__附求期望讲解方法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal p

hdu 4405 Aeroplane chess 概率dp入门题

Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are 1,2,3

Aeroplane chess(简单概率dp)

Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are 1,2,3,4,5,6). Whe

HDU 4405:Aeroplane chess(概率DP入门)

http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six f

hdu 4405 Aeroplane chess (概率DP+求期望)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1551    Accepted Submission(s): 1063 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to