描述:
Aeroplane chess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3430 Accepted Submission(s): 2180
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.
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.
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
Recommend
zhoujiaqi2010 | We have carefully selected several similar problems for you:
5867
5866
5865
5864
5863
题意:
跳棋有0~n个格子,每个格子X可以摇一次色子,色子有六面p(1=<p<=6),概率相等,可以走到X+p的位置,有些格子不需要摇色子就可以直接飞过去。问从0出发到达n或超过n摇色子的次数的期望。
思路:
简单概率dp,去年网络赛的一道水题,当时水平太差没过。
dp[i]表示从i出发到达最终位置的次数期望,所以需要逆推。
转移方程当i需要摇色子时,dp[i]=Σ(1+dp[i+j])/6(1<=j<=6);否则dp[i]=dp[jump[i]] ,其中jump[i]表示从i能够跳得到的最大位置。
预处理后面的6个位置,直接转移就行。
全期望公式:http://zh.wikipedia.org/wiki/%E5%85%A8%E6%9C%9F%E6%9C%9B%E5%85%AC%E5%BC%8F
全概率公式:http://zh.wikipedia.org/wiki/%E5%85%A8%E6%A6%82%E7%8E%87%E5%85%AC%E5%BC%8F
概率期望学习:http://kicd.blog.163.com/blog/static/126961911200910168335852/
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,m;
double dp[N];
int jump[N],path[N];
int main(){
while(~scanf("%d%d",&n,&m)&&(n||m)){
memset(path, -1, sizeof(path));
memset(jump, -1, sizeof(jump));//jump[i]表示从i能够飞的最大的位置
for(int i=1; i<=m; i++){
int a,b;scanf("%d%d",&a,&b);
path[a]=b;//记录飞一步到达的位置
}
for(int i=n; i>=1; i--){
if(path[i]!=-1){
int j=path[i];
if(jump[j]!=-1)jump[i]=jump[j];
else jump[i]=j;
}
}
for(int i=0; i<6; i++)dp[n+i]=0;
for(int i=n-1; i>=0; i--){
if(jump[i]!=-1)dp[i]=dp[jump[i]];
else{
double tmp=0;
for(int j=1; j<=6; j++){
tmp+=dp[i+j]*(1.0/6.0);
}
dp[i]=1+tmp;
}
}
printf("%.4lf\n",dp[0]);
}
return 0;
}