题意:飞行棋,从0到n,置骰子,置到几就往前走几步,前进中会有捷径,比如2和5连到一起了,那你走到2时可以直接跳到
5,如果5和8连到一起了,那你还可以继续跳到8,最后问跳到n时平均置几次骰子。也就是求期望。
/* 又是一道看了题解感觉很显然,但是怎么都想不出来的题目。 dp[i]表示i到n的期望步数,如果i这个地方能传送到j,那么dp[i]=dp[j],否则等几率到达一些点。 */ #include<cstdio> #include<iostream> #include<cstring> #define N 100010 using namespace std; double dp[N];int to[N],n,m; int main(){ while(1){ memset(to,-1,sizeof(to)); memset(dp,0,sizeof(dp)); scanf("%d%d",&n,&m); if(!n&&!m) break; for(int i=1;i<=m;i++){ int x,y;scanf("%d%d",&x,&y); to[x]=y; } dp[n]=0; for(int i=n-1;i>=0;i--){ if(to[i]!=-1) dp[i]=dp[to[i]]; else { for(int j=1;j<=6;j++) dp[i]+=dp[i+j]/6.0; dp[i]+=1.0; } } printf("%.4lf ",dp[0]); } return 0; }