HDU 4405 Aeroplane chess

题意:玩飞行棋,棋盘有0~n共n+1,每次抛一个6面的骰子,若得到x(1<=x<=6)点,则从i走到i+x格。其中,有些格子是飞机场,只要站在第A格上,就会从第A格直接飞到第B格。当你站在第k格且k >= n时,游戏结束。求抛骰子次数的期望。

解法:就是很简单的概率DP加了一点限制条件,用map处理一下就好。具体看代码。

tag:math, 概率DP

 1 /*
 2  * Author:  Plumrain
 3  * Created Time:  2013-10-30 11:17
 4  * File Name: DP-HDU-4405.cpp
 5  */
 6 #include<iostream>
 7 #include<cstdio>
 8 #include<cstring>
 9 #include<map>
10 #include<set>
11 #include<utility>
12 
13 using namespace std;
14 
15 #define CLR(x) memset(x, 0, sizeof(x))
16 const int N = 100000;
17 
18 int n, m;
19 double e[N+5];
20 map<int, int> mp;
21 
22 void init()
23 {
24     mp.clear();
25     int t1, t2;
26     for (int i = 0; i < m; ++ i){
27         scanf ("%d%d", &t1, &t2);
28         mp[t1] = t2;
29     }
30 
31     for (int i = 0; i < n; ++ i) if (mp.count(i)){
32         int t = mp[i];
33         while (mp.count(t))
34             t = mp[t];
35         mp[i] = t;
36     }
37 }
38 
39 double DP()
40 {
41     double p = (double)1 / 6;
42     CLR (e);
43     for (int i = n-1; i >= 0; -- i){
44         e[i] = 1;
45         if (mp.count(i)) continue;
46         for (int j = 1; j <= 6; ++ j){
47             if (mp.count(i+j))
48                 e[i] += e[mp[i+j]] * p;
49             else
50                 e[i] += e[i+j] * p;
51         }
52     }
53 
54     int ret = 0;
55     while (mp.count(ret))
56         ret = mp[ret];
57     return e[ret];
58 }
59 
60 int main()
61 {
62     while (scanf ("%d%d", &n, &m) != EOF && n){
63         init();
64         printf ("%.4f\n", DP());
65     }
66     return 0;
67 }
View Code

 

posted @ 2013-10-30 12:18  Plumrain  阅读(221)  评论(0编辑  收藏  举报