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 ...
随机推荐
- [安卓] 6、列表之ArrayAdapter适配
这个和以前的几个都有点不同,首先这个不用在xml中写对应的控件,而是直接在activity中将整个list实现的:首先要实例化列表和用于存储数据的数组list[9-10],第12-14行放list里加 ...
- 记一次在StackOverFlow上问问题的经历
最近一直在做测试方面的事情,被测的一些功能需要连接到FTP服务器上.而我在做本地测试时为了方便,就使用java写了一个简单的ftp服务器,可以在命令行下直接启动运行. 当时在main函数里是这样写的. ...
- 苹果App Store审核指南中文翻译(2014.9.1更新)
转:http://www.cocoachina.com/appstore/20140901/9500.html CocoaChina对<苹果应用商店审核指南>中文翻译最近一次更新时间为20 ...
- hdu 2191 多重背包 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
http://acm.hdu.edu.cn/showproblem.php?pid=2191 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...
- ubuntu下安装 infer
sudo apt-get updatesudo apt-get upgradesudo apt-get install git openjdk-7-jdk m4 zlib1g-dev python-s ...
- iframeWin For Easy UI. 为 Easy UI 扩展的支持IFrame插件
iframeWin For Easy UI. 为 Easy UI 扩展的支持IFrame插件 在一个项目中用了Easy UI,但是发现里面的 Dialog .Window.Messager 弹窗都不支 ...
- gson 自定义对象转换格式
有时候我们希望gson按照我们想要的方式转换,比如将日期转换为时间戳 class GsonBuilderUtil { public static Gson create() { GsonBuilder ...
- 比较好的文件复制工具fastcopy和校验工具
fastcopy http://ipmsg.org/tools/fastcopy.html.en extractfile --可以选用ADLER32计算模式,更快速.
- MongoDB副本集配置系列十一:MongoDB 数据同步原理和自动故障转移的原理
1:数据同步的原理: 当Primary节点完成数据操作后,Secondary会做出一系列的动作保证数据的同步: 1:检查自己local库的oplog.rs集合找出最近的时间戳. 2:检查Primary ...
- 修改oracle密码有效期限制
racle11g,静默安装后用户的密码有效期默认设置为180天,180天后密码将失效,oracle会提示要修改密码. 我们项目用的是jdbc连接oracle数据库,没法自动处理oracle的这种密 ...