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

Description

Download as PDF

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;
}

 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值