POJ 3519 Minimal Backgammon
| Time Limit: 5000MS | Memory Limit: 65536K | |||
| Total Submissions: 1195 | Accepted: 700 | Special Judge | ||
Description
![]() Figure 2: An example game |
Here is a very simple variation of the game backgammon, named “Minimal Backgammon”. The game is played by only one player, using only one of the dice and only one checker (the token used by the player).
The game board is a line of (N + 1) squares labeled as 0 (the start) to N (the goal). At the beginning, the checker is placed on the start (square 0). The aim of the game is to bring the checker to the goal (square N). The checker proceeds as many squares as the roll of the dice. The dice generates six integers from 1 to 6 with equal probability.
The checker should not go beyond the goal. If the roll of the dice would bring the checker beyond the goal, the checker retreats from the goal as many squares as the excess. For example, if the checker is placed at the square (N − 3), the roll “5” brings the checker to the square (N − 2), because the excess beyond the goal is 2. At the next turn, the checker proceeds toward the goal as usual.
Each square, except the start and the goal, may be given one of the following two special instructions.
Lose one turn (labeled “L” in Figure 2)
If the checker stops here, you cannot move the checker in the next turn.Go back to the start (labeled “B” in Figure 2)
If the checker stops here, the checker is brought back to the start.
Given a game board configuration (the size N, and the placement of the special instructions), you are requested to compute the probability with which the game succeeds within a given number of turns.
Input
The input consists of multiple datasets, each containing integers in the following format.
N T L B
Lose1
⋯
LoseL
Back1
⋯
BackB
N is the index of the goal, which satisfies 5 ≤ N ≤ 100. T is the number of turns. You are requested to compute the probability of success within T turns. T satisfies 1 ≤ T ≤ 100. L is the number of squares marked “Lose one turn”, which satisfies 0 ≤ L ≤ N − 1. B is the number of squares marked “Go back to the start”, which satisfies 0 ≤ B ≤ N − 1. They are separated by a space.
Losei’s are the indexes of the squares marked “Lose one turn”, which satisfy 1 ≤ Losei ≤ N − 1. All Losei’s are distinct, and sorted in ascending order. Backi’s are the indexes of the squares marked “Go back to the start”, which satisfy 1 ≤ Backi ≤ N − 1. All Backi’s are distinct, and sorted in ascending order. No numbers occur both in Losei’s and Backi’s.
The end of the input is indicated by a line containing four zeros separated by a space.
Output
For each dataset, you should answer the probability with which the game succeeds within the given number of turns. The output should not contain an error greater than 0.00001.
Sample Input
6 1 0 0
7 1 0 0
7 2 0 0
6 6 1 1
2
5
7 10 0 6
1
2
3
4
5
6
0 0 0 0
Sample Output
0.166667
0.000000
0.166667
0.619642
0.000000
题目大意:第一行输入N,T,L,B,表示总共有编号为0-N的一排格子,N号格子为目标格子,其中有L个格子走到那些格子上要停止一次,B个格子,走到那些格子上就要直接返回到0号格子,紧接着输入L个数字表示要停止一次的格子的编号,后面是B个数字,表示要返回到0号格子的格子的编号,每一轮抛一枚骰子,骰子上是多少就前进几步,如果超出了编号为N的格子,则开始后退,问在T轮之内到达目标格子的概率。
解题方法:概率DP,dp[i][j]代表第i轮走到第j号格子的概率。
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std; double dp[][];//dp[i][j]代表第i轮走到第j号格子的概率 int main()
{
int Back[], Lose[], N, T, L, B, index;
while(scanf("%d%d%d%d", &N, &T, &L, &B) != EOF)
{
if (N == && T == && L == && B == )
{
break;
}
memset(dp, , sizeof(dp));
memset(Back, , sizeof(Back));
memset(Lose, , sizeof(Lose));
for (int i = ; i < L; i++)
{
scanf("%d", &index);
Lose[index] = ;//要停止一次的格子的编号
}
for (int i = ; i < B; i++)
{
scanf("%d", &index);
Back[index] = ;//要返回的格子的编号
}
dp[][] = ;
for (int i = ; i < T; i++)
{
for (int j = N - ; j >= ; j--)
{
if (dp[i][j] == )//如果第i轮到达j号格子的概率为0,则直接忽略
{
continue;
}
if (Back[j] == )
{//走到j号格子要返回0号格子,则第i步走到0号格子的概率加上dp[i][j]
dp[i][] += dp[i][j];
}
else
{
if (Lose[j] == )//如果走到j号格子要停止一次,直接跳过第i + 1轮
{
for (int k = ; k <= ; k++)
{
if (j + k <= N)
{
dp[i + ][j + k] += dp[i][j] / ;
}
else
{//如果走到超过最大值N,则从N开始后退
dp[i + ][ * N - j - k] += dp[i][j] / ;
}
}
}
else
{
for (int k = ; k <= ; k++)
{
if (j + k <= N)
{
dp[i + ][j + k] += dp[i][j] / ;
}
else
{
dp[i + ][ * N - j - k] += dp[i][j] / ;
}
}
}
}
}
}
double ans = ;
//从第1轮到第T轮到达终点的概率之和,因为每一轮都有可能到达终点
for (int i = ; i <= T; i++)
{
ans += dp[i][N];
}
printf("%.6f\n", ans);
}
return ;
}
POJ 3519 Minimal Backgammon的更多相关文章
- {POJ}{3925}{Minimal Ratio Tree}{最小生成树}
题意:给定完全无向图,求其中m个子节点,要求Sum(edge)/Sum(node)最小. 思路:由于N很小,枚举所有可能的子节点可能情况,然后求MST,memset()在POJ G++里面需要cstr ...
- POJ 1815 Friendship
Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissions: 10626 Accepted: 2949 Descr ...
- [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)
BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...
- POJ 1325 Machine Schedule——S.B.S.
Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13731 Accepted: 5873 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- poj 1266 Cover an Arc.
http://poj.org/problem?id=1266 Cover an Arc. Time Limit: 1000MS Memory Limit: 10000K Total Submiss ...
- POJ 3414 Pots
Pots Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- poj 2594 Treasure Exploration (二分匹配)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 6558 Accepted: 2 ...
- poj 1651 Multiplication Puzzle (区间dp)
题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...
随机推荐
- C#移除HTML标记
移除一段文字中的HTML标记,以消除其中包含的样式和段落等,最常用的办法可能就是正则表达式了.但是请注意,正则表达式并不能处理所有的HTML文档,所以有时采用一个迭代的方式会更好,如for循环.看下面 ...
- duilib进阶教程 -- 扩展duilib的消息 (11)
duilib并没有提供双击和右键消息,所以需要我们自行扩展,这里以添加双击消息为例, 在UIDefine.h里,我们只看到了DUI_MSGTYPE_CLICK消息,却没有看到双击消息,因此需要在这里添 ...
- 利用BitLocker和vhdx创建一个有加密的Win10系统
如果电脑不支持TPM加密BitLocker,就无法对系统盘进行全盘加密. 可以采用一个变通的方法:创建一个vhdx,将这个虚拟磁盘进行BitLocker加密,然后在这个盘里安装操作系统,最后把vhdx ...
- 使用AJAX填充<select>标签下拉项,没有显示指定的option项
newCarInfo.js代码如下: $(function() { // 获取燃油种类 url = "basicFuelType_queryAll.action"; ...
- JVM中的Stack和Frame
JVM执行Java程序时需要装载各种数据,比如类型信息(Class).类型实例(Instance).常量数据(Constant).本地变量等.不同的数据存放在不同的内存区中,这些数据内存区称作“运行时 ...
- 你的项目真的需要Session吗?
在web开发中,Session这个东西一直都很重要,至少伴随我10年之久, 前一段时间发生一个性能问题,因为Redis session 问题,后来想想 其实我的项目session 是不需要的. 先看看 ...
- GTD时间管理(1)---捕获搜集
前一段时间感觉自己的整个思路很混乱,每一天觉得自己有很多事情很多,但是坐着做着不知道自己做了多少,做项目的时候做着做着时常东想西想.我个人觉得这种想法是不对经的. 于是在google上都出去寻找这方面 ...
- JAVA “Run as administrator” “UAC disabled” alternative solution
Technorati 标签: psexec,run as administrator,UAC java.io.IOException: Cannot run program "psexec. ...
- iphone/ipad/ipod设置VPN(pptp连接方式)
一.点击桌面上的-设置-图标进入设置(如图) 二.点击-通用-进入通用设置 三.点击-VPN-进入VPN设置(如图) 四.点击添加VPN设置进行设置 五.选择并连接
- Windows无法安装到GPT分区形式磁盘的解决办法
现在很多新买的硬盘都是GTP格式,这种格式需要使用UEFI BIOS模式安装系统,我们以前传统的windows系统安装都是“MBR+legacy BIOS”模式安装 Windows无法安装到GPT分区 ...
