UV-1541 to bet or not to bet (DP + probability)

Source: Internet
Author: User

Description

Alexander Charles McMillan loves to gamble, and during his last trip to the casino he ran into ss 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

Pushed. 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. or bet against. message .)

Sample Input
54 40 0 0 03 30 -1 L3 40 -1 L3 50 -1 L10 20+1 0 0 -1 L L 0 +3 -7 0
Sample output
Bet. 0.9375bet against. 0.20.push. 0.5000bet. 0.7500bet. 0.8954 question: there are 0 to m + 1 grids, so that you can go from 0 to m + 1. The 1 to M grids have corresponding information and take n steps, take n steps back, don't move, no information. You can throw a coin each time. Take two steps on the front and one step on the back, the thought of thinking about the probability of moving to the lattice m + 1 within a t-round: DP, set DP [I] [J] to indicate the probability that the number of cells can reach the J-level in the I-round. Each time, the probability is calculated by taking two or one step.
#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;}

 

UV-1541 to bet or not to bet (DP + probability)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.