您的位置:首页 > 运维架构

hdu 4405 Aeroplane chess(简单概率dp 求期望)

2014-11-04 20:14 501 查看

Aeroplane chess

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1667 Accepted Submission(s): 1123




[align=left]Problem Description[/align]

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


题意:

有一个飞行棋n个格子,刚开始在0这个位置,每次可以扔色子,扔到x则可以移动x格
如果到达的位置>=n则胜利
另外在棋盘上还有m对点x,y表示在位置x可以直接飞行到y
求到达胜利平均的扔色子次数

分析:

期望逆推。

d[i]表示到达 i 的时候还需要几次才能到达n。

初始d
= 0; 而d[0]就是所求的答案

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <algorithm>
#define LL __int64
const int maxn = 1e5+10;
using namespace std;
double d[maxn];
int c[maxn];

int main()
{
int n, m, i, j;
while(~scanf("%d%d", &n, &m))
{
if(n==0 && m==0) break;
memset(c, -1, sizeof(c));
for(i = 0; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
c[a] = b;
}
memset(d, 0, sizeof(d));
d
= 0;
double t = 1.0/6;
for(i = n-1; i >= 0; i--)
{
if(c[i]==-1)
d[i] += 1.0 + d[i+1]*t + d[i+2]*t+d[i+3]*t+d[i+4]*t+d[i+5]*t+d[i+6]*t;
else
d[i] = d[c[i]];
}
printf("%.4f\n", d[0]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: