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 ...
随机推荐
- IM系统中如何保证消息的可靠投递(即QoS机制)
消息的可靠性,即消息的不丢失和不重复,是im系统中的一个难点.当初qq在技术上(当时叫oicq)因为以下两点原因才打败了icq:1)qq的消息投递可靠(消息不丢失,不重复)2)qq的垃圾消息少(它 ...
- VHDL_LIB之DFF
1 D-Flip-Flop with async reset or set library IEEE; use ieee.std_logic_1164.all; entity FFD is gener ...
- @RestController无法自动注入的问题
今天在练习spring boot的时候,发现在ide中无法将@RestController注入到代码中,@RestController注解依赖的包是org.springframework.web,检 ...
- sql server 的 isnull 函数
- 「CorelDRAW降价提醒」,您关注的商品已降价!
不管是“光棍节”还是“剁手节” 似乎和我都没有什么关系 事实证明,我错了 今天,早上竟然有不识趣的人发红包祝我单身快乐 纳尼,有没有搞错? 我能直接怼回去,说不领么? 但好像又不是我的风格 哎,一个红 ...
- Java中的自动转换
特点: 1. 系统自动完成的,不需要程序员手动修改代码 2.将 取值范围小的类型 自动提升为 取值范围大的类型 注意: 整数类型直接写会默认为int 小数类型直接写默认为double 类型的范围大小 ...
- 机器学习PAI快速入门
什么是机器学习? 机器学习(Machine Learning, ML)是一门多领域交叉学科,涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多门学科.专门研究计算机怎样模拟或实现人类的学习行为,以 ...
- bootstrapvalidator使用,重置校验
1.html页面需要注意的是验证字段需要用form-group包裹.需要引用相应的css和js. <form id="jobForm" role="form&quo ...
- Windows自调试Redis
一.安装Redis 1. Redis官网下载地址:http://redis.io/download,下载相应版本的Redis,在运行中输入cmd,然后把目录指向解压的Redis目录. 2.启动服务命令 ...
- js和java中URI的编码和解码
js中对文字进行编码主要有三个函数:escape,encodeURI,encodeURIComponent: 对应解码为:unescape,decodeURI,decodeURIComponent 这 ...