1. 程式人生 > >Aeroplane chess(期望DP)

Aeroplane chess(期望DP)

題目連結

直接遞推就行了,如果能從x飛到y,相當於dp[x]=dp[y],特判一下就行。

AC程式碼:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
typedef long long ll;
typedef pair<int, int> pii;
inline int read(){int r=0;char c=getchar();while
(c<'0'||c>'9') {c=getchar();}while(c>='0'&&c<='9') {r=r*10+c-'0';c=getchar();}return r;} inline ll readll(){ll r=0;char c=getchar();while(c<'0'||c>'9') {c=getchar();}while(c>='0'&&c<='9') {r=r*10+c-'0';c=getchar();}return r;} const double eps = 1e-8; const double PI = acos
(-1.0); const ll INFLL = 0x3f3f3f3f3f3f3f3f; const int INF = 0x3f3f3f3f; const int mod = 1e9+7; const int MAXN = 1e5; const int MAXM = 1e5; double dp[MAXN]; int fly[MAXN]; int main(int argc, char const *argv[]) { int n, m; while(cin >> n >> m && n+m){ memset(dp, 0, sizeof
dp); memset(fly, 0, sizeof fly); int x, y; for(int i=0; i<m; ++i){ cin >> x >> y; fly[x] = y; } for(int i=n-1; i>=0; --i){ if(fly[i]){ dp[i] = dp[fly[i]]; } else{ dp[i] = 1.0/6*(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6]) + 1.0; } } printf("%.4lf\n", dp[0]); } return 0; }