HDU 4405 Aeroplane chess (概率DP)

题意:从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n。

析:概率DP,dp[i] 表示从 i  这个位置到达 n 要掷的次数的数学期望。然后每次掷的数就是1-6,概率都相等为1/6,再特殊标记一下飞行点,那么就容易写过了,

在的时候是必须飞过去,不能掷骰子。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} double dp[maxn];
map<int, int> mp; int main(){
while(scanf("%d %d", &n, &m) == 2 && m+n){
mp.clear();
memset(dp, 0, sizeof dp);
for(int i = 0; i < m; ++i){
int a, b;
scanf("%d %d", &a, &b);
mp[a] = b;
}
for(int i = n-1; i >= 0; --i)
if(mp.count(i)) dp[i] += dp[mp[i]];
else{
for(int j = 1; j < 7; ++j)
dp[i] += dp[i+j] / 6.0;
dp[i] += 1.0;
}
printf("%.4f\n", dp[0]);
}
return 0;
}

  

上一篇:Codeforces 28C [概率DP]


下一篇:POJ 2151 Check the difficulty of problems (概率DP)