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的更多相关文章

  1. {POJ}{3925}{Minimal Ratio Tree}{最小生成树}

    题意:给定完全无向图,求其中m个子节点,要求Sum(edge)/Sum(node)最小. 思路:由于N很小,枚举所有可能的子节点可能情况,然后求MST,memset()在POJ G++里面需要cstr ...

  2. POJ 1815 Friendship

    Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 10626   Accepted: 2949 Descr ...

  3. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

  4. POJ 1325 Machine Schedule——S.B.S.

    Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13731   Accepted: 5873 ...

  5. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  6. poj 1266 Cover an Arc.

    http://poj.org/problem?id=1266 Cover an Arc. Time Limit: 1000MS   Memory Limit: 10000K Total Submiss ...

  7. POJ 3414 Pots

    Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  8. poj 2594 Treasure Exploration (二分匹配)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 6558   Accepted: 2 ...

  9. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

随机推荐

  1. C#与数据库访问技术总结(十八)

    ADO.NET 代码综合示例 前面已经介绍过OLE DB.NET和SQL Server.NET数据提供者可以用来连接不同的数据源. 以下代码不仅综合演示了使用ADO.NET的这两种数据提供者访问数据库 ...

  2. GCD中的dispatch_sync、dispatch_sync 分别与串行、并行队列组合执行小实验

    平常开发中会经常用gcd做一下多线程任务,但一直没有对同步.异步任务在串行.并行队列的执行情况做个全面的认识,今天写了个demo跑了下,还是有些新发现的. 代码如下: - (void)touchesB ...

  3. paip.java win程序迁移linux的最佳实践

    paip.java win程序迁移linux的最佳实践 1.class load路径的问题... windows哈第一的从calsses目录加载,,而linux优先从jar加载.. 特别的是修理了ja ...

  4. PLSQL查询表是否被锁定(转)

    PLSQL查询表是否被锁定(转) http://blog.sina.com.cn/s/blog_70717ff00100qb85.html (2011-05-08 13:13:06) 转载▼ 标签: ...

  5. Android MonoGame坑记

    1.Content 加载声音API错误: 2.TouchPanel.GetState()属于抢占式的,用一次后面不能继续使用,否则状态尚属第一次: 3.TouchPanel 坐标来自于硬件本身: 4. ...

  6. [原创]推荐一款强大的.NET程序内存分析工具.NET Memory Profiler

    [原创]推荐一款强大的.NET程序内存分析工具.NET Memory Profiler 1 官方网站:http://memprofiler.com/2 下载地址:http://memprofiler. ...

  7. win10蓝屏问题,关于驱动kisSaasUrlRedirectKnl64.sys 的

    上周末刚从win7升级到win10:今天出现了两次蓝屏了,都是显示: xxxxxxx 百度知道链接---http://zhidao.baidu.com/question/164141456570387 ...

  8. ORA-01033:ORACLE initialization or shutdown in progress

    借用他人的经验 客户Oracle服务器进入PL/SQL Developer时报ora-01033:oracle initializationg or shutdown in progress 错误提示 ...

  9. 从WEB SERVICE 上返回大数据量的DATASET

    前段时间在做一个项目的时候,遇到了要通过WEB SERVICE从服务器上返回数据量比较大的DATASET,当然,除了显示在页面上以外,有可能还要用这些数据在客户端进行其它操作.查遍了网站的文章,问了一 ...

  10. Java中long和Long有什么区别 (转载)

    “Long is a class. long is a primitive. That means Long can be null, where long can't. Long can go an ...