hdu4405Aeroplane chess【概率DP】
2015-10-13 18:39
309 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2 0
8 3
2 4
4 5
7 8
0 0
[align=left]Sample Output[/align]
1.1667
2.3441
[align=left]Source[/align]
2012 ACM/ICPC Asia Regional Jinhua Online
这个题也是蛮基础的 但是最后纠结于n-5~n这个范围的计算把自己整恶心了
其实要是从n-1一直推到0就很好地避开了这个问题 n-1 是由n推来
刚刚在纠结为毛要if(i+l<=n) dp[i]+=dp[i+l];其实不这么写也没问题→_→提交还快了11ms
但是要理解为毛这推 实际上就是在前一道题的基础上多了一个航线 可以直接飞 其他的 没区别了
/********
2015.10.12-2015.10.13
hdu4405
46MS 2916K 913B C++
31MS 2884K 931B C++
********/
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
double dp[100005];
int n,m,x,y;
int step[100005];
int main()
{
//freopen("cin.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
if(m==0&&n==0) break;
memset(dp,0,sizeof(dp));
memset(step,-1,sizeof(step));
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
step[x]=y;
}
for(int i=n-1;i>=0;i--)
{
if(step[i]!=-1) {
dp[i]=dp[step[i]];
continue;
}
for(int l=1;l<=6;l++)
{
// if(i+l<=n)
dp[i]+=dp[i+l];
//else break;
}
dp[i]=dp[i]/6+1;
}
printf("%.4f\n",dp[0]);
}
return 0;
}
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2 0
8 3
2 4
4 5
7 8
0 0
[align=left]Sample Output[/align]
1.1667
2.3441
[align=left]Source[/align]
2012 ACM/ICPC Asia Regional Jinhua Online
这个题也是蛮基础的 但是最后纠结于n-5~n这个范围的计算把自己整恶心了
其实要是从n-1一直推到0就很好地避开了这个问题 n-1 是由n推来
刚刚在纠结为毛要if(i+l<=n) dp[i]+=dp[i+l];其实不这么写也没问题→_→提交还快了11ms
但是要理解为毛这推 实际上就是在前一道题的基础上多了一个航线 可以直接飞 其他的 没区别了
/********
2015.10.12-2015.10.13
hdu4405
46MS 2916K 913B C++
31MS 2884K 931B C++
********/
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
double dp[100005];
int n,m,x,y;
int step[100005];
int main()
{
//freopen("cin.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
if(m==0&&n==0) break;
memset(dp,0,sizeof(dp));
memset(step,-1,sizeof(step));
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
step[x]=y;
}
for(int i=n-1;i>=0;i--)
{
if(step[i]!=-1) {
dp[i]=dp[step[i]];
continue;
}
for(int l=1;l<=6;l++)
{
// if(i+l<=n)
dp[i]+=dp[i+l];
//else break;
}
dp[i]=dp[i]/6+1;
}
printf("%.4f\n",dp[0]);
}
return 0;
}
相关文章推荐
- linux文件夹打包命令
- shell正则表达式
- hadoop命令行命令
- 在linux系统中的shell命令下如何访问一个url地址呢?
- OpenStack创建虚拟机,用SSH连接到实例
- copy_to_user()分析
- 构建Hadoop伪分布式环境
- Linux常用命令
- 用 GStreamer 简化 Linux 多媒体开发
- 如何编写systemctl自启动服务 .service文件
- [置顶] 利用openoffice+jodconverter-code-3.0-bate4 把ppt转图片
- linux系统之(一) 深入理解/proc文件系统
- linux源码分析之cpu初始化 kernel/head.s
- 17.1.4.3 Replication Slave Options and Variables 复制Slave 选项和变量
- Linux集群理论及技术
- 淘宝京东类电商评论标签化的思路
- SVN服务器环境搭建
- qemu源码架构
- hadoop集群环境多结点搭建
- Linux下Mysql 5.6.27 tar包安装实践