Aeroplane chess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3785 Accepted Submission(s): 2404
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.
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.
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
int t[MAXN];
double dp[MAXN];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)&&(n||m))
{
memset(t,0,sizeof(t));
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
t[x]=y;
}
for(int i=0;i<6;i++)
dp[n+i]=0;
for(int i=n-1;i>=0;i--)
{
if(t[i])
dp[i]=dp[t[i]];
else
{
double mid=0;
for(int j=1;j<=6;j++)
mid+=dp[i+j];
dp[i]=mid/6+1;
}
}
printf("%.4f\n",dp[0]);
}
return 0;
}