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

East Central North America 2000

题意:棋盘初始位置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;
}
				
时间: 2024-12-09 20:19:31

POJ1644: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 记忆化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

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 trie

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步的概率,然后

Gym 101174D Dinner Bet(概率DP)题解

题意:n个球,两个人每人选C个球作为目标,然后放回.每回合有放回的拿出D个球,如果有目标球,就实现了这个目标,直到至少一个人实现了所有目标游戏结束.问结束回合的期望.误差1e-3以内. 思路:概率DP,因为终止条件是目标数,那么A的目标数B的目标数是一定要有的,但是AB之间可能有交集,那么我就把他单独列出来,我们设dp[t][i][j][k]表示第t回合a有i个独有的没涂b有j个独有的没涂有k个共有的没涂.那么我们可以得到状态转移方程: $\LARGE{dp[t][ii][jj][kk] = d

Codeforces 28C [概率DP]

/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队伍的期望. 思路: 概率dp dp[i][j][k]代表前i个浴室有j个人最长队伍是k的概率. 枚举第i个浴室的人数.然后转移的时候其实是一个二项分布. */ #include<bits/stdc++.h> using namespace std; int jilu[55]; double dp[

hdu 3076 ssworld VS DDD (概率dp)

///题意: /// A,B掷骰子,对于每一次点数大者胜,平为和,A先胜了m次A赢,B先胜了n次B赢. ///p1表示a赢,p2表示b赢,p=1-p1-p2表示平局 ///a赢得概率 比一次p1 两次p0*p1 三次 p0^2*p1,即A赢的概率为p1+p*p1+p^2*p1+...p^n*p1,n->无穷 ///即a_win=p1/(1-p);b_win=p2/(1-p); ///dp[i][j]表示a赢了j次,b赢了i次的概率 ///dp[i][j]=dp[i-1][j]*b_win+dp[

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>

hdu4089(公式推导)概率dp

题意:有n人都是仙剑5的fans,现在要在官网上激活游戏,n个人排成一个队列(其中主角Tomato最初排名为m), 对于队列中的第一个人,在激活的时候有以下五种情况: 1.激活失败:留在队列中继续等待下一次激活(概率p1) 2.失去连接:激活失败,并且出队列然后排到队列的尾部(概率p2) 3.激活成功:出队列(概率p3) 4.服务器瘫:服务器停止服务了,所有人都无法激活了(概率p4) 求服务器瘫痪并且此时Tomato的排名<=k的概率. 解法:ans[i][j]表示i个人出于第j个位置要到目的状