博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3519 Minimal Backgammon
阅读量:4338 次
发布时间:2019-06-07

本文共 5609 字,大约阅读时间需要 18 分钟。

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 07 1 0 07 2 0 06 6 1 1257 10 0 61234560 0 0 0

Sample Output

0.1666670.0000000.1666670.6196420.000000 题目大意:第一行输入N,T,L,B,表示总共有编号为0-N的一排格子,N号格子为目标格子,其中有L个格子走到那些格子上要停止一次,B个格子,走到那些格子上就要直接返回到0号格子,紧接着输入L个数字表示要停止一次的格子的编号,后面是B个数字,表示要返回到0号格子的格子的编号,每一轮抛一枚骰子,骰子上是多少就前进几步,如果超出了编号为N的格子,则开始后退,问在T轮之内到达目标格子的概率。 解题方法:概率DP,dp[i][j]代表第i轮走到第j号格子的概率。
#include 
#include
#include
using namespace std;double dp[105][105];//dp[i][j]代表第i轮走到第j号格子的概率int main(){ int Back[105], Lose[105], N, T, L, B, index; while(scanf("%d%d%d%d", &N, &T, &L, &B) != EOF) { if (N == 0 && T == 0 && L == 0 && B == 0) { break; } memset(dp, 0, sizeof(dp)); memset(Back, 0, sizeof(Back)); memset(Lose, 0, sizeof(Lose)); for (int i = 0; i < L; i++) { scanf("%d", &index); Lose[index] = 1;//要停止一次的格子的编号 } for (int i = 0; i < B; i++) { scanf("%d", &index); Back[index] = 1;//要返回的格子的编号 } dp[0][0] = 1; for (int i = 0; i < T; i++) { for (int j = N - 1; j >= 0; j--) { if (dp[i][j] == 0)//如果第i轮到达j号格子的概率为0,则直接忽略 { continue; } if (Back[j] == 1) {
//走到j号格子要返回0号格子,则第i步走到0号格子的概率加上dp[i][j] dp[i][0] += dp[i][j]; } else { if (Lose[j] == 1)//如果走到j号格子要停止一次,直接跳过第i + 1轮 { for (int k = 1; k <= 6; k++) { if (j + k <= N) { dp[i + 2][j + k] += dp[i][j] / 6; } else {
//如果走到超过最大值N,则从N开始后退 dp[i + 2][2 * N - j - k] += dp[i][j] / 6; } } } else { for (int k = 1; k <= 6; k++) { if (j + k <= N) { dp[i + 1][j + k] += dp[i][j] / 6; } else { dp[i + 1][2 * N - j - k] += dp[i][j] / 6; } } } } } } double ans = 0; //从第1轮到第T轮到达终点的概率之和,因为每一轮都有可能到达终点 for (int i = 1; i <= T; i++) { ans += dp[i][N]; } printf("%.6f\n", ans); } return 0;}

 

转载于:https://www.cnblogs.com/lzmfywz/p/3265109.html

你可能感兴趣的文章
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>