UVA 1541 - To Bet or Not To Bet 记忆化DP概率
Alexander Charles McMillan loves to gamble, and during his last trip to the casino he ran across a new game. It is played on a linear sequence of squares as shown below.
A chip is initially placed on the Start square. The player then tries to move the chip to the End square through a series of turns, at which point the game ends. In each turn a coin is flipped: if the coin is heads the chip is moved one square to the right and if the coin is tails the chip is moved two squares to the right (unless the chip is one square away from the End square, in which case it just moves to the End square). At that point, any instruction on the square the coin lands on must be followed. Each instruction is one of the following:
1. Move right n squares (where n is some positive integer)
2. Move left n squares (where n is some positive integer)
3. Lose a turn
4. No instruction
After following the instruction, the turn ends and a new one begins. Note that the chip only follows the instruction on the square it lands on after the coin ip. If, for example, the chip lands on a square that instructs it to move 3 spaces to the left, the move is made, but the instruction on the resulting square is ignored and the turn ends. Gambling for this game proceeds as follows: given a board layout and an integer T, you must wager whether or not you think the game will end within T turns.
After losing his shirt and several other articles of clothing, Alexander has decided he needs professional help-not in beating his gambling addiction, but in writing a program to help decide how to bet in this game.
Input will consist of multiple problem instances. The first line will consist of an integer n indicating the number of problem instances. Each instance will consist of two lines: the rst will contain two integers m and T (1 <= m <= 50, 1 <= T <= 40), where m is the size of the board excluding the Start and End squares, and T is the target number of turns. The next line will contain instructions for each of the m interior squares on the board. Instructions for the squares will be separated by a single space, and a square instruction will be one of the following: +n, -n, L or 0 (the digit zero). The first indicates a right move of n squares, the second a left move of n squares, the third a lose-a-turn square, and the fourth indicates no instruction for the square. No right or left move will ever move you off the board.
Output for each problem instance will consist of one line, either
Bet for. x.xxxx
if you think that there is a greater than 50% chance that the game will end in T or fewer turns, or
Bet against. x.xxxx
if you think there is a less than 50% chance that the game will end in T or fewer turns, or
Push. 0.5000
otherwise, where x.xxxx is the probability of the game ending in T or fewer turns rounded to 4 decimal places. (Note that due to rounding the calculated probability for display, a probability of 0.5000 may appear after the Bet for. or Bet against. message.)
5
4 4
0 0 0 0
3 3
0 -1 L
3 4
0 -1 L
3 5
0 -1 L
10 20
+1 0 0 -1 L L 0 +3 -7 0
Bet for. 0.9375
Bet against. 0.0000
Push. 0.5000
Bet for. 0.7500
Bet for. 0.8954 题意: 给你一个长度为n的一维棋盘, 起点在0终点在n以后,每个棋盘格子上都有一个指示, 每次走之前 扔硬币判断走一步还是走两步 到相应的格子上 再按照指示,问你从在给定的T步内起点到终点的概率是多少 题解:我们对于dp[i][j] 表示当前在i格子,剩余能走j步,能走到终点的概率
记忆花递归就好了
每次有两种选择,每种选择有不同指示
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll; const int N = + ; int vis[N][N], destory[N], cool[N],n;
char str[N];
double dp[N][N];
double dfs(int d,int k) {
if(vis[d][k]) return dp[d][k];
if(d == n + ) return dp[d][k] = 1.0;
if(k <= ) return dp[d][k] = ;
double& ret = dp[d][k] = ;vis[d][k] = ;
int nex = d + ;
if(destory[nex])
ret += 0.5 * dfs(nex,k - );
else ret += 0.5 * dfs(nex + cool[nex],k - );
nex = d + > n + ? n+:d+;
if(destory[nex])
ret += 0.5 * dfs(nex,k - );
else ret += 0.5 * dfs(nex + cool[nex],k - );
return ret;
}
void init() {
memset(vis,,sizeof(vis));
memset(destory,,sizeof(destory));
memset(cool,,sizeof(cool));
}
int main() {
int T, cas = , m ,t;
scanf("%d",&T);
while(T--) {
init();
scanf("%d%d",&n,&t);
for(int i = ; i <= n; i++) {
scanf("%s",str);
if(str[] == 'L') destory[i] = ;
else sscanf(str, "%d", &cool[i]);
}
double ans = dfs(,t);
if (fabs(ans - 0.5) < 1e-)
printf("Push. 0.5000\n");
else if (ans > 0.5)
printf("Bet for. %.4lf\n", ans);
else
printf("Bet against. %.4lf\n", ans);
}
return ;
}
代码
UVA 1541 - To Bet or Not To Bet 记忆化DP概率的更多相关文章
- UVA - 11324 The Largest Clique 强连通缩点+记忆化dp
题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...
- AC自动机+全概率+记忆化DP UVA 11468 Substring
题目传送门 题意:训练指南P217 分析:没有模板串也就是在自动机上走L步,不走到val[u] == v的节点的概率 PS:边读边insert WA了,有毒啊! #include <bits/s ...
- UVa 10285 Longest Run on a Snowboard【记忆化搜索】
题意:和最长滑雪路径一样, #include<iostream> #include<cstdio> #include<cstring> #include <c ...
- 28.uva 10891 Game of Sum 记忆化dp
这题和上次的通化邀请赛的那题一样,而且还是简化版本... 那题的题解 请戳这里 ... #include<cstdio> #include<algorithm> #i ...
- UVA 1541 - To Bet or Not To Bet(概率递推)
UVA 1541 - To Bet or Not To Bet 题目链接 题意:这题题意真是神了- -.看半天,大概是玩一个游戏,開始在位置0.终点在位置m + 1,每次扔一个硬币,正面走一步,反面走 ...
- POJ-1644 To Bet or Not To Bet(概率DP)
To Bet or Not To Bet Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1668 Accepted: 541 D ...
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- UVA - 10118Free Candies(记忆化搜索)
题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...
- UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)
Problem UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...
随机推荐
- 什么是CAS?
CAS(Compare-and-Swap),即比较并替换,是一种实现并发算法时常用到的技术,Java并发包中的很多类都使用了CAS技术.CAS需要有3个操作数:内存地址V,旧的预期值A,即将要更新的目 ...
- RMS:均方根值,RMSE:均方根误差,MSE:标准差
.均方根值(RMS),有时也称方均根.效值.英语写为:Root Mean Square(RMS). 美国传统词典的定义为:The square root of the average of squar ...
- Selenium等待某个元素出现之隐式等待
找不到元素这个问题困扰了两天了,一直怀疑是页面div层次太多,定位不准确.于是就从table开始到最后一层精确定位,仍然找不元素.怎么办,在网上搜索答案,说是可以加个隐式试试,于是在执行前加了一句等待 ...
- mvc 伪静态任意扩展名的实现方法
比如:要实现 http://localhost:60291/home/geta/1212.html 或者 .abc 任意扩展名 完成两步即可. 第一步修改路由: public static void ...
- Data内置对象
1.内置对象 Date 日期对象 2.创建日期对象 2.1 根据当前的系统时间来创建日期对象. var date1 = new Date(); //a.输出日期对象的信息 console.log(da ...
- 读<<大数据时代>>的一些感想
第一次听说<<大数据时代>>这本书,是在网上看到的央视搞的一个2013中国好书评选活动推荐的25本“中国好书”的榜单中看到的.然后迅速上豆瓣上查看了一下对该书的评价,一看非常高 ...
- vue 子组件向父组件传值通信
父组件 子组件 子组件用this.$emit
- 【Five-Minute Share】“请先了解所使用的工具 ,磨刀不误砍柴工”
数据是应用系统的血液,没有数据的系统应用价值是非常有限的.经过多年的观察发现,身边很多的程序开发人员在开发应用系统的时候,都是按照标准SQL语法及应用方法去进行数据库设计,并进行应用开发的,没有任何的 ...
- linux网络路由配置
网卡配置文件介绍: # vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 (描述网卡对应的设备别名,例如ifcfg-eth0的文件中它为 ...
- caffe特征提取/C++数据格式转换
Caffe生成的数据分为2种格式:Lmdb 和 Leveldb 它们都是键/值对(Key/Value Pair)嵌入式数据库管理系统编程库. 虽然lmdb的内存消耗是leveldb的1.1倍,但是lm ...