标签:space 技术 std label iostream style splay n+1 alt
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.
2 0
8 3
2 4
4 5
7 8
0 0
1.1667
2.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 }
标签:space 技术 std label iostream style splay n+1 alt
原文地址:http://www.cnblogs.com/haoabcd2010/p/6701020.html