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. JavaScript实用技巧总结

    前言 总结一下最近接触到的JavaScript语法糖,与大家共享. 每块糖都有详细的说明和示例,就不多说了. 准确的类型检查 /* * @function: * 类型检查示例 * 通过此方法,可以检查 ...

  2. js继承精益求精之寄生式组合继承

    一.混合/组合继承的不足 上一篇JS继承终于混合继承,认真思考一下,发现其还是有不足之处的: 空间上的冗余:在使用原型链的方法继承父类的原型属性(Animal.prototype)的同时,也在子类的原 ...

  3. Win10年度更新开发必备:VS2015 Update 3正式版下载汇总

    微软在06月27日发布了Visual Studio 2015 Update 3 .在MSDN中微软也提供下载,而且MSDN的Visual Studio 2015 Update 3与官方免费下载的文件是 ...

  4. js里各浏览器解析XML,支持IE、火狐、Chrome等

    js在chrome中加载XML,js加载XML支持ff,IE6+,Opera等浏览器 见代码: <!doctype html> <html lang="en"&g ...

  5. [Java拾遗四]JavaWeb基础之Servlet_Request&&Response

    今天来回顾下之前学过Servle的Resquest以及Response的知识.1,Request和Response技术:    rr的作用:        request是请求,封装用户的请求信息.若 ...

  6. atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7

    atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7 1. 实现原理 1 2. 大的文件上传原理::使用applet 1 3. 新的bp 2 1. 性能提升---分割小文件上传 ...

  7. Leetcode 200 Number of Islands DFS

    统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS class Solution { public: int m, n; b ...

  8. Spring之事件发布系统

    springboot应用,启动spring容器大致有如下几个过程: 容器开始启动 初始化环境变量 初始化上下文 加载上下文 完成 对应的Spring应用的启动器的监听器可以监听以上的过程,接口如下: ...

  9. git 日志格式化

    alias.lg=log --graph --pretty=format:'%Cred%H%Creset @%C(yellow)%d%Creset %n Author: %cn <%ce> ...

  10. Linux/Unix 怎样找出并删除某一时间点的文件(转)

    在Linux/Unix系统中,我们的应用每天会产生日志文件,每天也会备份应用程序和数据库,日志文件和备份文件长时间积累会占用大量的存储空间,而有些日志和备份文件是不需要长时间保留的,一般保留7天内的文 ...