HDU4405--Aeroplane chess(概率dp)
题目
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
大意
有1~n,n个点,你从0号点开始跳,每次投骰子,点数是几你就向前走几步,然后有一些连接点x和点y的通道,这样你走到点x时会被直接传送到点y(如果点y又和点z间有个通道,你就会继续被传送到z,通道是单向的而且是从小的点传送到大的点(不会给你传回去) 求走到点n或比点n大的点就会停止,求停止时平均的投骰子次数
算是见过的比较简单的概率dp了,设dp[i]表示已经走到点i时到点n的期望投骰子数,
则如果i和某个点a之间有通道时
否则的话
很简单的递推公式了
然后会有多组数据,这算是个槽点吧
代码
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int res=0;
char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) res=(res<<1)+(res<<3)+(ch^48),ch=getchar();
return res;
}
double dp[100010];
int n,m,to[100005],x,y;
int main(){
n=read(),m=read();
while(n!=0||m!=0)
{
memset(dp,0,sizeof(dp));
memset(to,0,sizeof(to));
for(int i=1;i<=m;i++)
{
x=read(),y=read();to[x]=y;
}
dp[n]=0.0;
for(int i=n-1;i>=0;i--){
if(to[i])
{
dp[i]=dp[to[i]];continue;
}
else dp[i]=dp[i+1]/6+dp[i+2]/6+dp[i+3]/6+dp[i+4]/6+dp[i+5]/6+dp[i+6]/6+1.0;
}
printf("%.4lf",dp[0]);
puts("");
n=read(),m=read();
}
}