hdu 4405概率dp
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <algorithm> #include <queue> #include <vector> using namespace std; const int maxe = 50000; const int maxn = 100500; const int INF = 0x3f3f3f; double dp[maxn]; int map[maxn]; int main() { //freopen("E:\\acm\\input.txt","r",stdin); int N,M; while(cin>>N>>M && N+M){ for(int i=N;i<=N+5;i++) dp[i] = 0; memset(map,0,sizeof(map)); for(int i=1;i<=M;i++){ int x,y; scanf("%d %d",&x,&y); map[x] = y; } for(int i=N-1;i>=0;i--){ if(map[i]){ dp[i] = dp[map[i]]; continue; } int a[7]; double sum = 0; for(int j=1;j<=6;j++){ a[j] = i+j; while(map[a[j]]){ a[j] = map[a[j]]; } sum += dp[a[j]] + 1; } dp[i] = sum/6; } printf("%.4lf\n",dp[0]); } }