2015
05-24

Aeroplane chess

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.

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.

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.

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

1.1667
2.3441

5，如果5和8连到一起了，那你还可以继续跳到8，最后问跳到n时平均置几次骰子。也就是求期望。

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;
const int N=100005;

struct node
{
int y,next;
};

bool vis[N];
node path[N];
int first[N],t;
double dp[N];

{
path[t].y=y;
path[t].next=first[x];
first[x]=t++;
}

int main()
{
double s;
int n,m,v;
while(cin>>n>>m)
{
if(m==0&&n==0) break;
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
memset(first,0,sizeof(first));
int x,y;
t=1;
while(m--)
{
cin>>x>>y;
}
dp[n]=-1;
for(int i=n; i>=0; i--)
{
if(!vis[i])
{
vis[i]=true;
s=0;
for(int k=1; k<=6; k++)
s+=dp[i+k];
s/=6;
dp[i]+=(s+1);
}
for(int k=first[i]; k; k=path[k].next)
{
v=path[k].y;
dp[v]=dp[i];
vis[v]=true;
}
}
printf("%.4lf\n",dp[0]);
}
return 0;
}