首页 > 代码库 > Aeroplane chess(简单概率dp)
Aeroplane chess(简单概率dp)
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.
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 should output a line indicating the expected dice throwing times. Output should be rounded to 4 digits after decimal point.
Sample input
2 08 32 44 57 80 0
Sample Output
1.16672.3441
题意:一种飞行棋,起点为 0 ,掷一次骰子,可以走有 1-6 步,飞到 >= n 就赢了,有 m 个可以直接飞的点,n,m 都为 0 输入结束 ,问需要掷骰子的次数期望
题解:简单概率dp
逆推即可,还有就是,飞的起点和终点的期望相同
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 using namespace std; 6 7 #define MAXN 100010 8 9 int far[MAXN];10 double dp[MAXN];11 12 int main()13 {14 int n,m;15 while (scanf("%d%d",&n,&m)&&(n||m))16 {17 memset(far,-1,sizeof(far));18 while (m--)19 {20 int x,y;21 scanf("%d%d",&x,&y);22 far[x]=y;23 }24 memset(dp,0,sizeof(dp));25 26 for (int i=n;i>=0;i--)27 {28 if (far[i]==-1)29 {30 for (int j=1;j<=6;j++)31 dp[i]+=dp[i+j]/6.0;32 dp[i]+=1.0;33 if (i==n) dp[i]=0.0;34 }35 else dp[i]=dp[far[i]];36 }37 printf("%.4lf\n",dp[0]);38 }39 return 0;40 }
Aeroplane chess(简单概率dp)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。