Aeroplane chess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1503 Accepted Submission(s): 1025
There are also M flight lines on the chess map. The i-th flight line can help Hzz fly from grid Xi to Yi (0<Xi<Yi<=N) without throwing the dice. If there is another flight line from Yi, Hzz can take the flight line continuously. It is granted that there is no two or more flight lines start from the same grid.
Please help Hzz calculate the expected dice throwing times to finish the game.
Each test case contains several lines.
The first line contains two integers N(1≤N≤100000) and M(0≤M≤1000).
Then M lines follow, each line contains two integers Xi,Yi(1≤Xi<Yi≤N).
The input end with N=0, M=0.
2 0 8 3 2 4 4 5 7 8 0 0
1.1667 2.3441
学习概率DP推荐一个链接:http://kicd.blog.163.com/blog/static/126961911200910168335852/
思路:由当前点能够走向以下6个相邻位置,走到这几个点的概率均相等。用dp[i]表示该点走到目标的期望步数,则该点的期望能够由它能够到达的6个点相加得到,由于它走到下一个位置花费时间1,故要加一。见式子:
dp[0]=dp[1]*1/6+dp[2]*1/6+dp[3]*1/6+dp[4]*1/6+dp[5]*1/6+dp[6]*1/6+1; dp[n]=0(自身到自身期望为0)
那么,我们倒着推过来就能得到答案为dp[0]。
#include"stdio.h" #include"string.h" #include"iostream" #include"algorithm" #include"math.h" #include"vector" using namespace std; #define LL __int64 #define N 100005 #define max(a,b) (a>b?a:b) vector<int>g[N]; int vis[N]; double dp[N]; int main() { int n,m,i,j,v,a,b; while(scanf("%d%d",&n,&m),n||m) { for(i=0;i<=n;i++) g[i].clear(); for(i=0;i<m;i++) { scanf("%d%d",&a,&b); g[b].push_back(a); } memset(dp,0,sizeof(dp)); //易知dp[n]=0 memset(vis,0,sizeof(vis)); for(i=0;i<g[n].size();i++) { v=g[n][i]; dp[v]=dp[n]; vis[v]=1; } for(i=n-1;i>=0;i--) { if(!vis[i]) { for(j=i+1;j<=i+6;j++) { dp[i]+=dp[j]/6; } dp[i]+=1; } for(j=0;j<g[i].size();j++) { v=g[i][j]; dp[v]=dp[i]; vis[v]=1; } } printf("%.4f ",dp[0]); } return 0; }