UVA - 1541 To Bet or Not To Bet (DP+概率)

Description

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

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

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.)

Sample Input

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

Sample Output

Bet for. 0.9375
Bet against. 0.0000
Push. 0.5000
Bet for. 0.7500
Bet for. 0.8954
题意:有0到m+1个格子,让你从0走到m+1,其中1到m的格子都有对应的信息,走n步,后退n步,不动,没有信息,每次你都可以抛硬币,正面走2步,反面走1步,求在不超过t轮内走到
m+1的格子的概率
思路:DP的思维,设dp[i][j]表示能在i轮内走到第j格的概率,每次要么走2步,要么1步,分别计算概率
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 100;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;

int m, t;
int num[maxn];
double dp[maxn][maxn];

void input() {
	char str[maxn];
	scanf("%d%d", &m, &t);
	memset(dp, 0, sizeof(dp));
	num[0] = num[m+1] = 0, num[m+2] = -1;

	for (int i = 1; i <= m; i++) {
		scanf("%s", str);
		if (str[0] == 'L')
			num[i] = inf;
		else sscanf(str, "%d", &num[i]);
	}
	dp[0][0] = 1.0;
}

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		input();
		for (int i = 0; i < t; i++) {
			for (int j = 0; j <= m; j++) {
				if (num[j+1] == inf)
					dp[i+2][j+1] += dp[i][j] * 0.5;
				else dp[i+1][j+1+num[j+1]] += dp[i][j] * 0.5;
				if (num[j+2] == inf)
					dp[i+2][j+2] += dp[i][j] * 0.5;
				else dp[i+1][j+2+num[j+2]] += dp[i][j] * 0.5;
			}
		}

		double ans = 0.0;
		for (int i = 0; i <= t; i++)
			ans += dp[i][m+1];
		if (fabs(ans-0.5) < eps)
			printf("Push. 0.5000\n");
		else if (ans < 0.5)
			printf("Bet against. %.4lf\n", ans);
		else printf("Bet for. %.4lf\n", ans);
	}
	return 0;
}

时间: 2024-08-03 20:04:29

UVA - 1541 To Bet or Not To Bet (DP+概率)的相关文章

uva 1541 - To Bet or Not To Bet(记忆化+概率)

题目链接:uva 1541 - To Bet or Not To Bet 题目大意:在一个棋盘上进行游戏,给定棋盘长度m,不算起始和终止,以及走的步数t.从起点开始,每轮可以丢一枚硬币,正面移动2步,方面移动1步:中间的格子有写操作,包括移动一定步数,停止一次操作.问说在t步内到达终点的概率. 解题思路:dp[i][j]表示走到第i格用掉j步的概率,然后记忆化搜索,因为保证状态重复,并且可以确定递归终止条件. #include <cstdio> #include <cstring>

UVA 1541 - To Bet or Not To Bet(概率递推)

UVA 1541 - To Bet or Not To Bet 题目链接 题意:这题题意真是神了- -,看半天,大概是玩一个游戏,开始在位置0,终点在位置m + 1,每次扔一个硬币,正面走一步,反面走两步,走到的步上有4种情况: 1.向前走n步 2.向后走n步 3.停止一回合 4.无影响 问能在t次机会内,走到终点m + 1(如果跃过也算走到了)的概率,大于0.5,等于0.5,小于0.5对应不同输出 思路:题意懂了就好办了,其实就是递推就可以了dp[i][j]表示第i次机会,落在j步的概率,然后

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 th

POJ1644:To Bet or Not To Bet(概率DP)

Description 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 trie

【UVa】Headmaster&#39;s Headache(状压dp)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1758 晕....状压没考虑循环方向然后错了好久.. 这点要注意...(其实就是01背包变成了完全背包QAQ 我们将课程拆成两个点,然后状压 那么答案就是(1<<(s<<1))-1 转移就不说了,,,,,太简单.. #include <cstdio> #in

UVA 1025 A Spy in the Metro(DP)

Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examining the time table. The Algorithms City Metro consists of a s

UVA - 10817 Headmaster&#39;s Headache (状压dp+记忆化搜索)

题意:有M个已聘教师,N个候选老师,S个科目,已知每个老师的雇佣费和可教科目,已聘老师必须雇佣,要求每个科目至少两个老师教的情况下,最少的雇佣费用. 分析: 1.为让雇佣费尽可能少,雇佣的老师应教他所能教的所有科目. 2.已聘老师必须选,候选老师可选可不选. 3.dfs(cur, subject1, subject2)---求出在当前已选cur个老师,有一个老师教的科目状态为 subject1,有两个及以上老师教的科目状态为 subject2的情况下,最少的雇佣费用. dp[cur][subje

UVA - 10917 Walk Through the Forest (最短路+DP)

题意:Jimmy打算每天沿着一条不同的路走,而且,他只能沿着满足如下条件的道路(A,B):存在一条从B出发回家的路径,比所有从A出发回家的路径都短,你的任务是计算有多少条不同的路径 从后往前找最短路, 对于每一步要更新之后走的位置值: #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<algorithm> using namespace s

uva 11825 ,Hacker&#39;s Crackdown 状态压缩 dp

// uva 11825 Hacker's Crackdown // // 题目意思看了很久才看懂,有n台计算机,有n种服务,每台计算机上运行所有 // 的服务,并且其中有的计算机与某些计算机相互邻接,对于每台计算机, // 你可以选择一项服务,停止这项服务,则与它邻接的计算机的该服务也停止了 // 你的目的是让经量多的服务完全瘫痪 // // 换而言之,这个问题就是在n个集合中(p[1]....p[n])分成尽量多的组数,使得每组 // 的并集等于全集(即所有的n台电脑都停止)... // /