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 ...
随机推荐
- C#服务控件UpdatePanel的局部刷新与属性AutoPostBack回传
C#服务控件许多都有AutoPostBack这一属性(AutoPostBack意思是自动回传,也就是说此控件值更改后是否和服务器进行交互),如下代码所示: protected void Textbox ...
- mysql5.7 设置密码
5.7版本的MYSQL的用户表的密码字段与以往版本有所区别, 不再是password字段 而是authentication_string字段. 请注意修改密码: vim /opt/mysql_d/m ...
- JavaScript实现延时提示框
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- Python 之 基础知识(二)
一.分支运算 在Python 2.x中判断不等于还可以用<> if语句进阶:elif if 条件1: ...... elif 条件2: ...... else: ...... 二.逻辑运算 ...
- Struts2框架学习(三)——配置详解
一.struts.xml配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts ...
- H5 微信公众号 监听返回事件
/*-----监听返回事件-----*/ function pushHistory(returnUrl,currentUrl,currentTitle) { window.addEventListen ...
- WP 图片加载时显示加载中
private BitmapImage srcimage = new BitmapImage(); public MainPage() { InitializeComponent(); progres ...
- div与div之间的拖拽
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 【udacity】机器学习
Evernote Export 2.人工智能简介 机器学习源自于人工智能,在此方向上,该领域有分为不同学派,机器学习主要关注的是制造能够自主动作的机器 3.人工智能难题 1.所有智能体都只有很少的计算 ...
- jsmind实现思维导图,和echars 的tree图类似
https://blog.csdn.net/qq_41619796/article/details/88552029