标签:and ati int style help content ever rom row
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 0 8 3 2 4 4 5 7 8 0 0
Sample Output
1.1667 2.3441
本题题意:数轴上有N+1个点(编号0~N),一个人玩游戏,从0出发,当到达N或大于N的点则游戏结束。
每次行动掷骰子一次,骰子编号1-6,掷到多少就向前走几步,这个数轴上还有些特殊点,这些点类似
飞行棋中的飞行点,只要到达这些点就可以直接飞到给定点。求总共投掷骰子次数的期望。
题解:如果直接可以飞行就f[i]=f[reach],然后就是递推吧。
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cmath> #define ll long long #define N 100007 int n,m; double f[N]; int go_to[N]; int main() { while(~scanf("%d%d",&n,&m)&&(n+m)) { memset(go_to,-1,sizeof(go_to)); memset(f,0,sizeof(f)); int x,y; for(int i=1;i<=m;i++) { scanf("%d%d",&x,&y); go_to[x]=y; } for(int i=n-1;i>=0;i--) { if(go_to[i]==-1) { for(int j=1;j<=6;j++){ f[i]+=f[i+j]/6.0; } f[i]+=1; } else f[i]=f[go_to[i]]; } printf("%.4lf\n",f[0]); } }
HDU4405-Aeroplane chess(概率DP求期望)
标签:and ati int style help content ever rom row
原文地址:http://www.cnblogs.com/fengzhiyuan/p/7673883.html