pku 1644 To Bet or Not To Bet(DP)
来源:互联网 发布:钉钉办公软件 linux 编辑:程序博客网 时间:2024/05/08 18:51
To Bet or Not To Bet
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 1316 Accepted: 414
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.
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.)
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
54 40 0 0 03 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
题意:像大富翁一样...规矩是,有可能一次走一步或者走两步,格子有进n步,退n补,罚停一轮...
题解:设dp【i】【j】代表i轮走到j格的概率是多少,0格是起点,中间有m格,m+1格是终点,根据题意和数据可以知道,它不会越界...(其实是我测了数据,才觉得它不会直接走出终点,但是事实上我觉得它会啊....或者没有这样的数据吧,反正AC了- -不管了)
#include<stdio.h>#include<string.h>#define INF 0xffffffdouble dp[57][57];int mark[57];char s[8];int main(){ int cas,m,t,i,j; scanf("%d",&cas); while(cas--) { scanf("%d%d",&m,&t); memset(mark,0,sizeof(mark)); memset(dp,0,sizeof(dp)); for(i=1;i<=m;i++) { scanf("%s",s); if(s[0]=='0') mark[i]=0; else if(s[0]=='L') mark[i]=INF; else { for(j=1;s[j]!='\0';j++) //一开始j写成了i...测了好一会儿 mark[i]=mark[i]*10+s[j]-'0'; if(s[0]=='-') mark[i]=(-1)*mark[i]; } } for(mark[m+2]=-1,dp[0][0]=1,i=0;i<=t;i++) { for(j=0;j<=m;j++) { if(mark[j+1]==INF) dp[i+2][j+1]+=dp[i][j]*0.5; else dp[i+1][j+1+mark[j+1]]+=dp[i][j]*0.5; if(mark[j+2]==INF) dp[i+2][j+2]+=dp[i][j]*0.5; else dp[i+1][j+2+mark[j+2]]+=dp[i][j]*0.5; } } for(i=0;i<t;i++) dp[t][m+1]+=dp[i][m+1]; //t步以内到达的概率都要加起来 if(dp[t][m+1]>0.5) printf("Bet for. %.4lf\n",dp[t][m+1]); else if(dp[t][m+1]==0.5) printf("Push. 0.5000\n"); else printf("Bet against. %.4lf\n",dp[t][m+1]); } return 0;}
- pku 1644 To Bet or Not To Bet(DP)
- Poj 1644 To Bet or Not To Bet (概率 DP)
- POJ-1644 To Bet or Not To Bet(概率DP)
- UVA - 1541 To Bet or Not To Bet (DP+概率)
- POJ1644:To Bet or Not To Bet(概率DP)
- 1644 To Bet or Not To Bet 解题报告
- poj 1644 To Bet or Not To Bet
- poj 1644 To Bet or Not To Bet
- poj 1644 To Bet or Not To Bet(递推)
- 菜鸟学算法之POJ 1644 To Bet Or Not To Bet
- UVA 1541 - To Bet or Not To Bet(概率递推)
- uva 1541 - To Bet or Not To Bet(记忆化+概率)
- 10 Technologies To Bet On in the Recession
- 【HPUoj】Bet(数论)
- Codeforces Gym 101174 D. Dinner Bet (期望 + DP)
- Codeforces Round #400 (Div. 1 + Div. 2, combined) 776F. Sherlock's bet to Moriarty(待翻译)
- Bet(思维
- Codeforces-273(div2) A. Initial Bet
- [代码]solrcloud中 SolrCore 初始化
- NYOJ117-求逆序数(归并排序)
- 机器学习理论与实战(一)K近邻法
- 二叉树遍历(递归算法和非递归算法)
- 香港-夏季不宜 10~11 最好
- pku 1644 To Bet or Not To Bet(DP)
- [代码] 如何处理添加和删除文档请求
- 又听张楚、何勇
- Asian Handicap 亚洲让球盘
- linux遍历文件夹下的文件
- 发现一款感觉很不错的学英语软件:Rosseta Stone
- 一梦红尘,别问是劫是缘
- 判定一下设备是否为iphone5:
- J2EE (七) 配置文件详解以及dom4j进行解析