To Bet or Not To Bet

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 1668 Accepted: 541

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

概率DP题目,

递推即可,

#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <string> using namespace std;
#define MAX 999999
char a[55];
int m,t;
double dp[55][55];
int b[55];
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
memset(dp,0,sizeof(dp));
memset(b,0,sizeof(b));
scanf("%d%d",&m,&t);
for(int i=1;i<=m;i++)
{
scanf("%s",a); if(a[0]=='L')
b[i]=MAX;
else
sscanf(a,"%d",&b[i]);
}
b[0]=0;b[m+1]=0;b[m+2]=-1;
dp[0][0]=1.0;
for(int i=0;i<t;i++)
{
for(int j=0;j<m+1;j++)
{
if(b[j+1]==MAX)
dp[i+2][j+1]+=dp[i][j]*0.5;
else
dp[i+1][j+b[j+1]+1]+=dp[i][j]*0.5;
if(b[j+2]==MAX)
dp[i+2][j+2]+=dp[i][j]*0.5;
else
dp[i+1][j+b[j+2]+2]+=dp[i][j]*0.5;
}
}
double ans=0;
for(int i=0;i<=t;i++)
ans+=dp[i][m+1];
if(ans>0.5)
printf("Bet for. %.4f\n",ans);
else if(ans==0.5)
printf("Push. 0.5000\n");
else if(ans<0.5)
printf("Bet against. %.4f\n",ans); }
return 0;
}

最新文章

  1. 搭建一套自己实用的.net架构(4)【CodeBuilder-RazorEngine】
  2. python基础-基本数据类型
  3. NOI2005 聪聪和可可
  4. YL-64 颜色传感器
  5. 易出错的C语言题目之二:指针
  6. SASS语法备忘
  7. Spring boot 1.3.5 RELEASE 官方文档中文翻译--目录
  8. node.js 访问sql server的 node_modules “msnodesql&quot;的安装编译方法
  9. 从linux telnet到exchange邮件server来測试发送邮件
  10. 解决.net的堆碎片化带来的内存占用过大的问题
  11. hdu 3333 Turing Tree(线段树+离散化)
  12. Git Bash 简单操作
  13. 代码中输入数字自动筛选出最大值,使用array,for loop and if (21.9.2017)
  14. Node.js 实用工具
  15. openlayers4 入门开发系列之地图导航控件篇(附源码下载)
  16. Xcode9,cocoaPod执行pod install时报错,一行命令即可解决。
  17. dbForge Studio for MySQL 中文乱码问题
  18. ucore-lab1-练习6report
  19. Android实现图片的压缩、旋转工具类
  20. Java Core - Class文件结构之魔数、版本号、常量池

热门文章

  1. 解决 iOS7 通过tag 找不到 UITableViewCell 的子控件(转)
  2. js中===与==区别
  3. Django SimpleCMDB WSGI
  4. MQTT-C-PUB
  5. 虚拟机上的centos连不了外网,吧原来的配置信息改成如下就行(删除了一些多余的信息,变化:原来的ONBOOT的值是no)
  6. 【技术分享会】 @第五期 angularjs
  7. git 命令自动补全
  8. linux mint 安装 SecureCRT
  9. jquery validate使用笔记
  10. Linux下应急工具