能確定的狀態就是最終n-n+5這6個點的步數是0 然後從後往前遞推
#include #include #include #include #include #include #include template inline bool rd(T &ret) { char c; int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1; } template inline void pt(T x) { if (x <0) { putchar('-'); x = -x; } if(x>9) pt(x/10); putchar(x%10+'0'); } using namespace std; typedef long long ll; const int N = 100010; int n, m; double dp[N]; int to[N]; double solve(){ for(int i = 0; i < 6; i++) dp[i+n] = 0; for(int i = n-1; i >= 0; i--) { if(to[i]!=-1) dp[i] = dp[to[i]]; else { dp[i] = 1.0; for(int j = 1; j <= 6; j++) dp[i] += dp[i+j] / 6.0; } } return dp[0]; } void input(){ memset(to, -1, sizeof to); while(m--){ int x, y;rd(x); rd(y); to[x] = y; } for(int i = n-1; i >= 0; i--){ if(to[i] == -1)continue; if(to[to[i]] != -1) to[i] = to[to[i]]; } } int main() { while(cin>>n>>m, n+m){ input(); printf("%.4f\n", solve()); } return 0; } /* */
/* *程序的版權和版本聲明部分: *Copyrigh
題目描述: 從有序順序表中刪除所有其值重
1459: Chess Time Limit: 1
樹的判定 時間限制:1000 ms | 內存限制
指定它們指向哪個變量,如:
LeetCode 14 Longest Common Pre