Aeroplane chess
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1394 accepted submission (s): 944
Problem descriptionhzz 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, 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.
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 shocould output a line indicating the expected dice throwing times. Output shocould be rounded to 4 digits after decimal point.
Sample input2 08 32 44 57 80 0
Sample output1.16672.3441
Source2012 ACM/ICPC Asia Regional Jinhua online
Playing a Flying chess game has n + 1 grid numbered 0-N, starting from 0. Each time you shake the dice to a few, you can take a few steps and there are m jumpers, you can just jump when you are playing with yourself in the same color as your plane. Reach> = the expected number of times the dice are shaken at N positions.
Solution: Infinite recurrence, recursive or recursive memory-based search.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn=100010; 7 double p[maxn],e[maxn]; 8 int n,m; 9 int f[maxn];10 11 int main()12 {13 int i,j;14 while(scanf("%d%d",&n,&m),n+m)15 {16 memset(p,0,sizeof(p));17 memset(e,0,sizeof(e));18 memset(f,-1,sizeof(f));19 for(i=0;i<m;i++)20 {21 int x,y;22 scanf("%d%d",&x,&y);23 f[x]=y;24 }25 p[0]=1;26 for(i=0;i<n;i++)27 {28 if(f[i]==-1)29 {30 for(j=1;j<=6;j++)31 {32 p[i+j]+=p[i]/6;33 e[i+j]+=(p[i]+e[i])/6;34 }35 }36 else37 {38 p[f[i]]+=p[i];e[f[i]]+=e[i];39 }40 }41 double ans=0;42 for(i=0;i<6;i++)43 ans+=e[n+i];44 printf("%.4lf\n",ans);45 }46 return 0;47 }