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.
题意即为飞行棋,0-n共n+1格,m条航线可以从xi-yi,没有航线的话可以扔骰子后移动到当前位置+骰子点数的位置,当位置>=n时结束,求扔骰子的次数期望
如果该地有航线dp[i]=dp[y[i]]直接继承期望,否则dp[i]=1/6*dp[i+1]+1/6*dp[i+2]+…+1/6*dp[i+6]+1;
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 100000 + 5;
double dp[maxn];
int air[maxn];
int main()
{
int n, m, x, y;
while(~scanf("%d%d", &n, &m)) {
if(n == m && n == 0) break;
memset(air, 0, sizeof(air));
dp[n] = 0.0;
for(int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
air[x] = min(n, y);
}
for(int i = n - 1; i >= 0; i--) {
if(air[i] > 0) dp[i] = dp[air[i]];
else {
dp[i] = 1.0;
for(int j = 1; j <= 6; j++)
dp[i] += 1.0 / 6 * dp[(i + j > n) ? n : i + j];
}
}
printf("%.4f\n", dp[0]);
}
}