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 ...
随机推荐
- linux Redis 5.0集群搭建
文档结构如下: Redis cluster 是redis的分布式解决方案,在3.0版本正式推出后,有效的解决了redis分布式方面的需求:当遇到单机内存,并发,流量等瓶颈是,可以采用cluster架构 ...
- BZOJ 1196 二分+Kruskal
思路: 二分答案 判一下能不能加 //By SirisuRen #include <cstdio> #include <cstring> #include <algori ...
- Android View事件分发与传递
在Android中,人们主要通过手指与系统交互.Android把所有的touch事件都被封装成MotionEvent来进行处理,其中包括了手指点击的位置,时间等信息.其事件类型主要包括:ACTION_ ...
- 仿QQ浏览器mac版官网主页 html+css3特效
这是一款超酷的CSS3动态背景动画特效,CSS3仿QQ浏览器官网彗星动画背景特效. 在线演示本地下载
- idea中git的运用
创建本地 Git 仓库 安装 Git 插件 将代码添加到 Git 的本地仓库 在 GitHub 中创建仓库
- Here comes Treble: A modular base for Android
On the Android team, we view each dessert release as an opportunity to make Android better for our u ...
- bzoj 1191: [HNOI2006]超级英雄Hero 网络流_残量网络
题目描述: 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的 多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确回答一道题后 ...
- js中标签字符串的拼接
//1.用单双引号拼接 var valueDemo = "111"; var htmlStrs1 = '<option selected="selected&quo ...
- 兼容IE的两端对齐
div+css布局实现2端对齐是我们网页排版中经常会使用到的,这篇文章将总结一下可以实现的方法: html结构 实现demo里面的div通过Css进行2端对齐. <div class=" ...
- win10 1809磁盘占用总是100%
快过年了,提前请假回家,装几台电脑公司备用.有个电脑装完系统开机很慢,开机完成之后电脑响应也很慢,于是打开任务管理器发现磁盘中用率一直是100%,然而程序读取数据的速度并不高. 解决思路: 关闭win ...