hdu 4405 Aeroplane chess

//2012金華網賽1006

求概率的題,這不是一道有無窮多種情況的概率題,所以不用推公式。很容易想到dp解法。 定義狀態很狀態轉移看代碼吧,都比較簡單

#include<stdio.h>   
#include<string.h>   
#include <cmath>
#include <iostream>
#include <map>
#include<algorithm>   
#define fr(i,s,n) for(int i=s;i<n;i++)
#define pf printf
#define sf scanf
#define fi freopen("in.txt","r",stdin)
#define cl(a) memset(a,0,sizeof(a))
using namespace std;
typedef __int64 ll;
const int N=1000010;
struct P{
    double p;   //恰好到達的概率 
    double step;  //平均步數
};
P dp[N];
map<int,int> mp;
int tot=1;
int n,m;
int dis[1010];

void init(){
    fr(i,0,n+1)
        dp[i].p=0,dp[i].step=0;
}
void hashed(int x,int y){
    mp[x]=tot;
    dis[tot++]=y;
}

void DP(){
    dp[0].step=0;
    dp[0].p=1;
    fr(i,0,n){
        if (mp[i]){
            dp[ dis[mp[i]]].step+=dp[i].step;
            dp[ dis[mp[i]]].p+=dp[i].p;
        }else{
            fr(j,1,7){
                if (i+j<=n){
                    dp[i+j].step+=(dp[i].step+dp[i].p)/6.0;
                    dp[i+j].p+=dp[i].p/6;
                }
                else dp[n].step+=(dp[i].step+dp[i].p)/6.0;
            }
        }
    }
    pf("%.4f\n",dp[n].step);
}

int main(){
        
    int x,y;
    while(1){
        init();
        mp.clear();
        tot=1;
        sf("%d%d",&n,&m);
        if (n==0&&m==0) break;
        fr(i,0,m){
            sf("%d%d",&x,&y);
            hashed(x,y);
        }
        DP();
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章