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.
InputThere 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.
OutputFor 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 题意:一种飞行棋,起点为 0 ,掷一次骰子,可以走有 1-6 步,飞到 >= n 就赢了,有 m 个可以直接飞的点,n,m 都为 0 输入结束 ,问需要掷骰子的次数期望 题解:简单概率dp
逆推即可,还有就是,飞的起点和终点的期望相同
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; #define MAXN 100010 int far[MAXN];
double dp[MAXN]; int main()
{
int n,m;
while (scanf("%d%d",&n,&m)&&(n||m))
{
memset(far,-,sizeof(far));
while (m--)
{
int x,y;
scanf("%d%d",&x,&y);
far[x]=y;
}
memset(dp,,sizeof(dp)); for (int i=n;i>=;i--)
{
if (far[i]==-)
{
for (int j=;j<=;j++)
dp[i]+=dp[i+j]/6.0;
dp[i]+=1.0;
if (i==n) dp[i]=0.0;
}
else dp[i]=dp[far[i]];
}
printf("%.4lf\n",dp[]);
}
return ;
}