HDU-4405 Aeroplane chess

               http://acm.hdu.edu.cn/showproblem.php?pid=4405      

看了一下这个博客http://kicd.blog.163.com/blog/static/126961911200910168335852/

            Aeroplane chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1336    Accepted Submission(s): 913


Problem Description
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.
 

 

Input
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.
 

 

Output
For 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
 

 

Source
思路:
n个方程,回带一下就行。e[n]=0,所有标号i大于n的期望值e[i]也为0.
比如n=3,m=0
所列方程为e[0]=1/6e[1]+1/6e[2]+1/6e[3]+1
                  e[1]=1/6e[2]+1/6e[3]+1
                  e[2]=1/6e[3]+1
                  e[3]=0
从0这个点可以到1,2,3,4,5,6这几个位置,由于大于等于3游戏结束,不会再有期望的投色子次数了,所以跳到3和大于3的格子里期望值也就都是0了。
所以我们列出n个方程后直接回带就能把e[0]求出来。如e[3]=0可以求出e[2]=1,已知了e[2]和e[3]就可以求出e[1],进而求出e[0].
解答:
如果可以飞的话 就是e[a]的期望等于e[b]的期望,a<b;
不可以的话就:e[i]=1.0/6*(e[i+1]+e[i+2]+e[i+3]+e[i+4]+e[i+5]+e[i+6])+1;
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int f[2000005];
double e[2000005];
int main()
{
     int i,a,b,n,m;
     while(~scanf("%d%d",&n,&m))
    {
       if(n==0&&m==0)
           break;
           memset(f,0,sizeof(f));
           memset(e,0,sizeof(e));
      for(i=1;i<=m;i++)
      {
          scanf("%d%d",&a,&b);
          f[a]=b;
      }
        e[n]=0;
      for(i=n-1;i>=0;i--)
      {
           if(f[i]!=0)
              e[i]=e[f[i]];
          else
          e[i]=1.0/6*(e[i+1]+e[i+2]+e[i+3]+e[i+4]+e[i+5]+e[i+6])+1;
      }
      printf("%.4lf\n",e[0]);
   }
    return 0;
}

 


    
 
 

你可能感兴趣的:(HDU)