hdu 4405 Aeroplane chess 概率DP
思路:简单的概率DP
dp[i]表示从i到n的期望步数:
当i没有飞机时dp[i]=∑p[k]*p[i+k]+1;
当i有飞机时dp[i]=dp[fly[i]].
代码如下:
1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 #include<iomanip> 5 #include<cmath> 6 #include<cstring> 7 #include<vector> 8 #define ll __int64 9 #define pi acos(-1.0) 10 #define MAX 100005 11 using namespace std; 12 double p[MAX]; 13 int fly[MAX]; 14 int main(){ 15 int n,i,j,t,k,m,a,b; 16 while(cin>>n>>m){ 17 if(m==0&&n==0) break; 18 memset(fly,0,sizeof(fly)); 19 memset(p,0,sizeof(p)); 20 for(i=0;i<m;i++){ 21 cin>>a>>b; 22 fly[a]=b; 23 } 24 for(i=n-1;i>=0;i--){ 25 if(fly[i]!=0) 26 p[i]=p[fly[i]]; 27 else{ 28 p[i]=(p[i+1]+p[i+2]+p[i+3]+p[i+4]+p[i+5]+p[i+6])/6+1; 29 } 30 } 31 printf("%.4lf\n",p[0]); 32 } 33 return 0; 34 }