HDU 4050 wolf5x(动态规划-概率DP)
wolf5x
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 402 Accepted Submission(s): 248
Special Judge
right leg next. As you can not jump , only one leg is allowed to use each step. Every step you take is in the range of [A,B], inclusively; namely, every step you take is at most B units and at least A units.
Before you start to move, the grids will be initialized randomly with 4 states(0,1,2,3), and p[i][j] means the probability of ith grid initialized with state j. After initialization, the state of the grids will not change.
State 0 means you can't step into the correspoding grid.
State 1 means you can just step into the grid with your left leg.
State 2 means you can just step into the grid with your right leg.
State 3 means you can step into the grid with either of your legs,and the next step,you can use any legs; namely you don't need to follow the rules above.
If x>n, then the grid can be stepped in with arbitrary method.means you can step at the place after the nth grid.
For every step,you will choose the “step method” with the minimum step length. Namely, if you can take the step of S units and S+1 units, you will choose the step of S units.
Until you can't step in any grids in front of you,or you have been in a grid x>n, you will stop.
Can you calculate the expectation of the steps when you stop?
For each case,the first line is three integers n,A,B.
The next n lines,each line has 4 number p[i][0], p[i][1], p[i][2], p[i][3].
1 <= A <= B <= n<= 2000.
0 <= p[i][j] <= 1, p[i][0]+p[i][1]+p[i][2]+p[i][3] = 1.
you can assume that the relative epsilon is no more than 1e-6
9
2 1 1
0 0.5 0.5 0
0 0 1 0
2 1 1
0 0.5 0.5 0
0.5 0.5 0 0
2 1 2
0 0.5 0.5 0
0 0 1 0
2 1 2
0.2 0.3 0.4 0.1
0.15 0.2 0.25 0.4
3 1 10
0 0 0 1
0 0 0 1
0 0 0 1
3 1 1
0 0 0 1
0 0 0 1
0 0 0 1
3 2 2
0 0 0 1
0 0 0 1
0 0 0 1
3 3 3
0 0 0 1
0 0 0 1
0 0 0 1
3 1 2
0.0 0.3 0.6 0.1
0.1 0.2 0.3 0.4
0.5 0.4 0.1 0.0
2.00000000
1.50000000
2.50000000
2.46000000
4.00000000
4.00000000
2.00000000
2.00000000
2.80200000
题目大意:
这是一维的,一个人在0号格子,现在1~n号格子排成一排,上面有各种限制,一个人想从 0号格子走出n号格子,也就是走到 >n 处。
每个格子是4种状态的其中一种,并且没告诉你是哪种状态,只是告诉你概率,第i号格子4种状态的其中一种的概率记为p[i][0],p[i][1],p[i][2],p[i][3]。
0 表示这个格子既不能左腿也不能右腿踏进去。
1 表示这个格子可以左腿踏进去。
2 表示这个格子可以右腿踏进去。
3 表示这个格子既可以左腿也可以右腿踏进去。
这个人刚开始可以选择迈出左腿也可以迈出右腿,但是下次必须迈出不同的腿,除非你走在3这个状态下,下次又可以左右腿选一个,就和走路一样。
这个人一次迈出的步子的范围是[A,B],还有就是如果他迈出的步子可以是A+1或者A+2或者别的,他会选择最少的步数也就是 A+1
问你,这个人走的步数的数学期望,这个人走到 >n 处停止或者不能再走了也停止。
解题思路:
看到了数学期望,我就想到了概率DP,而这题的解法也就是概率DP,这个赛区已经看到了三道DP题目了。
(1)首先,用DP(pos,state) 记录这个状态下你还需要走的步数的数学期望
其中 ,pos记录你当前所在的格子,state记录你可以迈出的步子
0 表示你下一步不能走了,也就需要走的步数的数学期望0,是所以不会有这个状态,直接被剪纸剪掉了。
1 表示下一步要用左腿踏进去
2 表示下一步要用右腿踏进去
3 表示下一步既左腿也可以右腿踏进去
那么我么要求的就是DP(0,3)表示在0号格子,下一步既左腿也可以右腿踏进去,这个状态下你还需要走的步数的数学期望
(2)关于递推方程怎么求
DP(pos,state) =sum { p * ( DP(newpos,newstate) + 1 ) }
解释下我写的状态转移方程:
比如说,你当前在s位置,那么就枚举下一步走的位置 i, i>=s+A 且 i<=s+B
你走这个位置的概率,就是前面都不能走,也就是 s~i-1都同时为0状态,因为如果不是0状态你就得走,题目交代他会选择最少的步数。
所以得维护 s~i-1都同时为0状态的概率,也就是前缀相乘。
那么,现在讨论如果走了第 i 号位置
如果你的这一步要用右腿踏进去,下一步就得用左腿,如果这个位置的状态是3,你下个位置随意。
左腿同理。
解题代码:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std; const int maxn=2100;
double p[maxn][5],dp[maxn][5];
int a,b,n,vis[maxn][5],marked; void input(){
scanf("%d%d%d",&n,&a,&b);
for(int i=1;i<=n;i++){
for(int j=0;j<4;j++){
scanf("%lf",&p[i][j]);
}
}
} double DP(int k,int state){
if(k>n) return 0;
if(k==n) return 1;
if(vis[k][state]==marked) return dp[k][state];
double ans=0;
if(state==3){
double p0=1.0;
for(int i=k+a;i<=k+b;i++){
if(i>n){
ans+=p0*1.0;
break;
}
ans+=p0*p[i][3]*(DP(i,3)+1);
ans+=p0*p[i][2]*(DP(i,1)+1);
ans+=p0*p[i][1]*(DP(i,2)+1);
p0*=p[i][0];
}
}else if(state==2){
double p0=1.0;
for(int i=k+a;i<=k+b;i++){
if(i>n){
ans+=p0*1.0;
break;
}
ans+=p0*p[i][3]*(DP(i,3)+1);
ans+=p0*p[i][2]*(DP(i,1)+1);
p0*=(p[i][0]+p[i][1]);
}
}else{
double p0=1.0;
for(int i=k+a;i<=k+b;i++){
if(i>n){
ans+=p0*1.0;
break;
}
ans+=p0*p[i][3]*(DP(i,3)+1);
ans+=p0*p[i][1]*(DP(i,2)+1);
p0*=(p[i][0]+p[i][2]);
}
}
vis[k][state]=marked;
return dp[k][state]=ans;
} void solve(){
marked++;
printf("%.8lf\n",DP(0,3));
} int main(){
int T;
scanf("%d",&T);
while(T-- >0){
input();
solve();
}
return 0;
}
版权声明:欢迎关注我的博客,本文为博主toyking原创文章,未经博主允许不得转载。
HDU 4050 wolf5x(动态规划-概率DP)的更多相关文章
- HDU 4336 Card Collector(动态规划-概率DP)
Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful card ...
- HDU 4089 Activation(概率DP)(转)
11年北京现场赛的题目.概率DP. 公式化简起来比较困难....而且就算结果做出来了,没有考虑特殊情况照样会WA到死的.... 去参加区域赛一定要考虑到各种情况. 像概率dp,公式推出来就很容易写 ...
- 动态规划——概率dp
所谓概率dp,用动态规划的思想找到一个事件中可能发生的所有情况,然后找到符合要求的那些情况数,除以总数便可以得到符合要求的事件发生的概率.其核心思想还是通过dp来得到事件发生的所有情况,很类似在背包专 ...
- HDU 4405 Aeroplane chess (概率DP)
题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i 这个位置到达 n ...
- HDU - 5001 Walk(概率dp+记忆化搜索)
Walk I used to think I could be anything, but now I know that I couldn't do anything. So I started t ...
- HDU 4050 wolf5x 概率dp 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=4050 题意: 现在主角站在0处,需要到达大于n的位置 主角要进入的格子有三种状态: 0. 不能进入 1. 能进入 ...
- HDU 2955 Robberies 背包概率DP
A - Robberies Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- HDU 4576 Robot (概率DP)
暴力DP求解太卡时间了...........写挫一点就跪了 //hdu robot #include <cstdio> #include <iostream> #include ...
- 2016ACM/ICPC亚洲区沈阳站H - Guessing the Dice Roll HDU - 5955 ac自动机+概率dp+高斯消元
http://acm.hdu.edu.cn/showproblem.php?pid=5955 题意:给你长度为l的n组数,每个数1-6,每次扔色子,问你每个串第一次被匹配的概率是多少 题解:先建成ac ...
随机推荐
- JS魔法堂:获取当前脚本文件的绝对路径
一.前言 当写模块加载器时,获取当前脚本文件的绝对路径作为基础路径是必不可少的一步,下面我们一起来探讨一下这个问题吧! 二.各大浏览器的实现方式 [a]. Chrome和FF 超简单的一句足矣! va ...
- QTableView表格视图的列宽设置
Qt中的表格控件可以通过从QTableView或QTableWidget派生子类实现.其中,QTableWidget只是对QTableView的一种简单封装.因为使用QTableView常常需要用户指 ...
- EF容器---代理类对象
#region 修改--官方的修改是,先查询,然后修改 /// <summary> /// 修改--官方的修改是,先查询,然后修改 /// </summary> static ...
- C#设计模式——观察者模式(Observer Pattern)1
一.概述在软件设计工作中会存在对象之间的依赖关系,当某一对象发生变化时,所有依赖它的对象都需要得到通知.如果设计的不好,很容易造成对象之间的耦合度太高,难以应对变化.使用观察者模式可以降低对象之间的依 ...
- 微信公众平台入门开发教程.Net(C#)框架
一.序言 一直在想第一次写博客,应该写点什么好?正好最近在研究微信公众平台开发,索性就记录下,分享下自己的心得,也分享下本人简单模仿asp.net运行机制所写的通用的微信公众平台开发.Net(c#)框 ...
- C#实现的18位身份证格式验证算法
18位身份证标准在国家质量技术监督局于1999年7月1日实施的GB11643-1999<公民身份号码>中做了明确的规定. GB11643-1999<公民身份号码>为GB1164 ...
- 线段树的区间更新---A Simple Problem with Integers
POJ 3468 Description 给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 100 ...
- 泛函编程(21)-泛函数据类型-Monoid
Monoid是数学范畴理论(category theory)中的一个特殊范畴(category).不过我并没有打算花时间从范畴理论的角度去介绍Monoid,而是希望从一个程序员的角度去分析Monoid ...
- [小北De编程手记] : Lesson 08 - Selenium For C# 之 PageFactory & 团队构建
本文想跟大家分享的是Selenium对PageObject模式的支持和自动化测试团队的构建.<Selenium For C#>系列的文章写到这里已经接近尾声了,如果之前的文章你是一篇篇的读 ...
- ASP.NET MVC中将控制器分离到类库的实现
前言 在ASP.NET MVC的开发中,我们创建完项目之后,ASP.NET MVC是已Model-Controller-View的形式存在的,在创建项目自动生成的内容上Model我们很容易分离成类库, ...