Poj1644:to bet or not to Bet (probability DP)

Source: Internet
Author: User

Description

Alexander Charles McMillan loves to gamble, and during him last trips 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 in which point the game ends. In each turn a coin is FL
Ipped:if The coin is heads the chip was moved one square to the right and if the the coin was tails the chip is moved the Squar Es to the right (unless the chip was one square away from the end square, in which case it just moves to the end square). At a, 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 is follows the instruction on the square it lands on after the coin flip. If, for example, the chip lands on a square this instructs it to move 3 spaces to the left, the move was made, but the Inst Ruction 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 would end within T turns.
After losing him shirt and several other articles of clothing, Alexander have decided he needs professional help-not in Bea Ting his gambling addiction, but in writing a program to help decide what to the bet in the this game.

Input

Input would consist of multiple problem instances. The first line would consist of an integer n indicating the number of problem instances. Each instance would consist of lines:the first would contain the integers m and T (1 <= m <=, 1 <= T <= , 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 would contain instructions for each of the m interior squares on the board. Instructions for the squares would be separated by a single space, and a square instruction would 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 th E Fourth indicates no instruction for the square. No right or left move would ever move you off the board.

Output

Output for each problem instance would consist of one line, either
Bet for. X.xxxx
If you think this there is a greater than 50% chance that the game would end in T or fewer turns, or
Bet against. X.xxxx
If you think there are a less than 50% chance that the game would 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 if appear after the Bet for. O R Bet against. Message.)

Sample Input

0 0 30-1 L3 40-1 L3 50-1 L10 20+1 0 0-1 l l 0 +3-7 0

Sample Output

Bet for. 0.9375Bet against. 0.0000Push. 0.5000Bet for. 0.7500Bet for. 0.8954

Source

East Central North America 2000

Test instructions: The initial position of the board 0, the end of the position of m+1, each round of coin to decide the right or two steps, the probability of each 0.5. The chessboard will have a description of +n,-n,l, or 0, which means to go to the right N-grid, go to the left N-Grid, lose a round of opportunity, without any explanation. Given the size of the chessboard, and the description of each lattice on the board, the probability of going to the end of the T-wheel or less than the T-wheel.


Idea: Probability DP, one step and two steps of the probability is equal, according to the next step to the position produced by the situation of DP can be


<pre name= "code" class= "CPP" > #include <iostream> #include <stdio.h> #include <string.h># Include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm>using namespace std; #define LS 2*i#define rs 2*i+1#define up (i,x,y) for (i=x;i <=y;i++) #define DOWN (i,x,y) for (i=x;i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) # Define LL long longconst double pi = ACOs ( -1.0); #define Len 100005#define mod 19999997const int INF = 0x3f3f3f3f;double DP [55]    [55],ans;int Step[55];char Str[55];int Main () {int t,m,n,i,j,k;    scanf ("%d", &n);        W (n--) {scanf ("%d%d", &m,&t);        MEM (step,0);        MEM (dp,0);        STEP[M+2] =-1;            Up (i,1,m) {scanf ("%s", str);            if (str[0]== ' L ') step[i] = INF;        else sscanf (str, "%d", &step[i]);        } Dp[0][0] = 1.0; Up (i,0,t-1) {up (j,0,m) {if (Step[j+1]==inf) dp[i+2][j+1] + = Dp[i]                [j]*0.5;                else dp[i+1][j+1+step[j+1]] + = dp[i][j]*0.5;                if (Step[j+2]==inf) dp[i+2][j+2] + = dp[i][j]*0.5;            else dp[i+1][j+2+step[j+2]] + = dp[i][j]*0.5;        }} ans = 0;        Up (i,0,t) ans+=dp[i][m+1]; if (ans==0.5) printf ("Push.        0.5000\n "); else if (ans>0.5) printf ("Bet for".        %.4f\n ", ans); else printf ("Bet against.    %.4f\n ", ans); } return 0;}



Poj1644:to bet or not to Bet (probability DP)

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.