hdu 4405 Aeroplane chess (概率DP)
2016-03-05 18:25
309 查看
Aeroplane chess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1503 Accepted Submission(s): 1025
Problem Description
Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are 1,2,3,4,5,6). When Hzz is
at grid i and the dice number is x, he will moves to grid i+x. Hzz finishes the game when i+x is equal to or greater than N.
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.
Input
There are multiple test cases.
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.
Output
For each test case in the input, you should output a line indicating the expected dice throwing times. Output should be rounded to 4 digits after decimal point.
Sample Input
2 0 8 3 2 4 4 5 7 8 0 0
Sample Output
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
=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 ; int vis ; double dp ; 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 =0 memset(vis,0,sizeof(vis)); for(i=0;i<g .size();i++) { v=g [i]; dp[v]=dp ; 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\n",dp[0]); } return 0; }
相关文章推荐
- CentOS hadoop 分布式集群的搭建
- 极路由 openwrt 使用 SyncY 实现百度云同步
- 安装 Apache 2.4
- Linux内核页表
- linux及安全第二周总结——20135227黄晓妍
- Linux /etc/rc.d 下面 rc${runlevel}.d rc.local init.d 区别
- Docker实践之自定义镜像、推送至远程docker hub
- Python IDLE或Python Shell不支持中文编码的解决方案
- linux dmesg命令参数及用法详解(linux显示开机信息命令)
- CentOS7 yum 安装git
- apache的三种工作模式
- Tomcat设置环境变量
- Linux3.5内核以后的路由下一跳缓存
- Windows系统下通过xmanager远程桌面控制Linux
- Linux多线程与进程之间通信 实例2
- CentOS6.5 64位安装单机版hadoop2.6教程
- Linux3.5内核以后的路由下一跳缓存
- linux搭建nfs服务器
- 20135327郭皓——Linux内核分析第二周 操作系统是如何工作的
- Linux 多线程与进程间通信 实例 1