HDU 4405:Aeroplane chess 概率DP求期望
One Person Game
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4405
题意:
有个人在玩飞行棋,规则是:掷一个骰子,就能从当前点x飞到(x+点数)处,在棋盘上有一些飞行通道,如果点x和点y间存在飞行通道,那么当你走到点x,就会飞到点y处,起点在0,求从起点飞到终点n所需要投掷骰子次数的期望。
题解:
一道简单的求期望的题,不会求期望的可以看下这里
当点i是飞行通道的起点的时候,由于不需要投掷骰子,就能飞到飞行通道的终点处,所以此时dp[i]=dp[飞行通道的终点]
当点i不是是飞行通道的起点的时候,在掷一次骰子后可以飞到i+点数处,所以此时dp[i]=∑dp[i+k]*1/6 +1
代码
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=100005;
double dp[N];
int q[N];
int main()
{
	  int n,m,x,y;
	  while(~scanf("%d%d",&n,&m)&&(n||m))
	  {
		    memset(q,0,sizeof(q)); 
		    memset(dp,0,sizeof(dp));
		    while(m--)
		    {
			      scanf("%d%d",&x,&y);
			      q[x]=y;
		    }
		    for(int i=n-1;i>=0;--i)
		    if(!q[i])
		    {
			      for(int j=1;j<=6;++j)
			      if(i+j<=n) dp[i]+=dp[i+j]/6.0;
			      dp[i]++;
		    }
		    else dp[i]=dp[q[i]];
		    printf("%.4f\n",dp[0]);
	  }
}
                    
                
                
            
        
浙公网安备 33010602011771号