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

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
Bet for. 0.9375
Bet against. 0.0000
Push. 0.5000
Bet for. 0.7500
Bet for. 0.8954 题意: 给你一个长度为n的一维棋盘, 起点在0终点在n以后,每个棋盘格子上都有一个指示, 每次走之前 扔硬币判断走一步还是走两步 到相应的格子上 再按照指示,问你从在给定的T步内起点到终点的概率是多少 题解:我们对于dp[i][j] 表示当前在i格子,剩余能走j步,能走到终点的概率
   记忆花递归就好了
   每次有两种选择,每种选择有不同指示
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll; const int N = + ; int vis[N][N], destory[N], cool[N],n;
char str[N];
double dp[N][N];
double dfs(int d,int k) {
if(vis[d][k]) return dp[d][k];
if(d == n + ) return dp[d][k] = 1.0;
if(k <= ) return dp[d][k] = ;
double& ret = dp[d][k] = ;vis[d][k] = ;
int nex = d + ;
if(destory[nex])
ret += 0.5 * dfs(nex,k - );
else ret += 0.5 * dfs(nex + cool[nex],k - );
nex = d + > n + ? n+:d+;
if(destory[nex])
ret += 0.5 * dfs(nex,k - );
else ret += 0.5 * dfs(nex + cool[nex],k - );
return ret;
}
void init() {
memset(vis,,sizeof(vis));
memset(destory,,sizeof(destory));
memset(cool,,sizeof(cool));
}
int main() {
int T, cas = , m ,t;
scanf("%d",&T);
while(T--) {
init();
scanf("%d%d",&n,&t);
for(int i = ; i <= n; i++) {
scanf("%s",str);
if(str[] == 'L') destory[i] = ;
else sscanf(str, "%d", &cool[i]);
}
double ans = dfs(,t);
if (fabs(ans - 0.5) < 1e-)
printf("Push. 0.5000\n");
else if (ans > 0.5)
printf("Bet for. %.4lf\n", ans);
else
printf("Bet against. %.4lf\n", ans);
}
return ;
}

代码

最新文章

  1. Kubernetes集群搭建过程中遇到的问题
  2. C# 进程间通信之二传递复杂数据类型(转)
  3. [ACM_动态规划] Alignment (将军排队)
  4. iOS 获取设备版本型号
  5. 如何让图片在垂直方向与 div的底部对齐 水平居中
  6. Pyhton 学习总结 21 :fileinput模块
  7. 网站性能Web压力测试工具webbench
  8. HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)
  9. Codeforces 231E - Cactus
  10. iOS_block内存分析
  11. WIN_2003_SP2.iso VMware 不能进行网络访问的处理
  12. javascript每日一练(二)——javascript(函数,数组)
  13. 合并BIN文件的两种方法(转)
  14. 适合精致女孩使用的APP软件 不容错过的精彩人生
  15. JavaScript用法
  16. 使用GraphHttpClient调用Microsoft Graph接口 - GET
  17. 【转】如何基于linux进程通信设计方案
  18. C++中的extern
  19. spring中的springSecurity安全框架的环境搭建
  20. php如何控制客户端生成缓存

热门文章

  1. Koa 中实现 chunked 数据传输
  2. 开发vue插件并发布到npm包管理工具的流程
  3. 前端-js进阶和JQ源码思维导图笔记
  4. JavaScript实现延时提示框
  5. [OpenWrt]安装mjpg-streamer
  6. V4L2框架之视频监控
  7. 读书笔记6-浪潮之巅(part1)
  8. max 宏定义取消:error C2589: error C2059: 语法错误 : “::”
  9. Python代码块缓存、小数据池
  10. bzoj 4372: 烁烁的游戏 动态点分治_树链剖分_线段树