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. [新概念51单片机C语言教程·郭天祥] 1、 基础知识必备

    目录: 单片机的大致介绍         1-1.通俗定义         1-2.51系列产品         1-3.标号意思         1-4.引脚介绍         1-5.用C语言开 ...

  2. JSPatch中的OC高级语法

    1)多线程相关 dispatch_after dispatch_async dispatch_sync dispatch_get_main_queue dispatch_get_global_queu ...

  3. 浅谈sql中的in与not in,exists与not exists的区别

    转 浅谈sql中的in与not in,exists与not exists的区别   12月12日北京OSC源创会 —— 开源技术的年终盛典 »   sql exists in 1.in和exists ...

  4. 连接数据库——模拟ATM机查、存、取、开户功能

    1.界面:包含开户.查询.存款.取款.功能 package com.bank.test; /** * * @author Administrator *界面类 */ public class Jiem ...

  5. SSL在https和MySQL中的原理思考

    之前对HTTPS通信过程有过了解,HTTPS是应用HTTP协议使用SSL加密的版本,在TCP和HTTP之间增加SSL协议.通过握手阶段认证双方身份,协商对称秘钥对通信信息进行加密.此处只描述常用的服务 ...

  6. 让delphi解析chrome扩展的native应用

    chrome浏览器自从去年以来逐步去掉了对浏览器插件的支持,npapi的方案马上不可用. 当务之急要选择一个替代方案,最常用的就是扩展了.扩展程序提供了一套和本地程序交互的方案——“原生消息通信” 写 ...

  7. MyBatis 查询

    User.java package com.mycom.mybatis_1.bean; import java.io.Serializable; public class User implement ...

  8. 谈一谈PHP的代码重构

    随着 PHP 从一种简单的脚本语言转变为一种成熟的编程语言,一个典型的 PHP 应用程序的代码库的复杂性也随之增大.为了控制对这些应用程序的支持和维护,我们可以使用各种测试工具来自动化该流程.其中一种 ...

  9. java微信开发API第一步 服务器接入

    I如何接入服务器,下面就为大家进行介绍 一.说明 * 本示例根据微信开发文档:http://mp.weixin.qq.com/wiki/home/index.html最新版(4/3/2016 5:34 ...

  10. swift入门篇-函数

    今天给大家介绍 swift函数,swift函数和c#,js的写法大致一直,但是与object-c写法有很大不同点.废话不多说,直接开始了. 1:函数  --常量参数 func 函数名( 参数变量:类型 ...