码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-23 23:30:27      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:poj   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 fl 
ipped: 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 flip. 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 first 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

Source



题意:棋盘初始位置0,结束位置m+1,每一轮抛硬币决定向右一步还是两步,概率各为0.5。棋盘每格上都会有一个说明+n,-n,L,或者 0,分别意味着向右走n格,向左走n格,失去一轮机会,无任何说明。给出棋盘的大小,以及棋盘上每一格的说明,求在t轮或者少于t轮便走到终点处的概率。


思路:概率DP,走一步和两步的概率是相等的,根据下一步走到的位置所产生的情况进行DP即可


<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 long
const double pi = acos(-1.0);
#define Len 100005
#define mod 19999997
const 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(概率DP)

标签:poj   dp   

原文地址:http://blog.csdn.net/libin56842/article/details/45231007

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!