Minimal Backgammon
Time limit1sMemory limit128 MB
Simulate turn by turn probability mass over board positions (with lose-a-turn and go-to-start squares, and bounce-back overshoot rule) to find probability of reaching the goal within T turns.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Simulation, Probability
- Solved
- No attempts yet
Problem
This is a very simple, single-player variant of backgammon called “Minimal Backgammon”. It is played by one player using a single die and a single checker (the player's token).
The board is a line of squares labeled (the start) through (the goal). At the beginning the checker is placed on the start (square ), and the aim is to bring the checker to the goal (square ). On each turn the player rolls the die, which shows each of the integers through with equal probability, and the checker advances by that many squares.
The checker must not go beyond the goal. If a roll would bring the checker beyond the goal, the checker instead moves up to the goal and then retreats from the goal by the number of squares in excess. For example, if the checker is on square , a roll of brings it to square , because the amount by which it would exceed the goal is . On the next turn it again proceeds toward the goal as usual.
Every square other than the start and the goal may carry one of the following two special instructions.
- Lose one turn: if the checker stops here, you cannot move the checker on the next turn.
- Go back to the start: if the checker stops here, the checker is immediately brought back to the start (square ).
Given a board configuration (the size and the placement of the special squares), compute the probability that the game succeeds (the checker reaches the goal) within a given number of turns.
Input
The input consists of multiple datasets. Each dataset has the following format.
N T L B
Lose_1
...
Lose_L
Back_1
...
Back_B
is the index of the goal and satisfies . is the number of turns; you must compute the probability of success within turns, and it satisfies . is the number of squares marked “Lose one turn” and satisfies . is the number of squares marked “Go back to the start” and satisfies . These four values are separated by spaces.
Each value in the Lose list is the index of a square marked “Lose one turn” and satisfies value ; the values are distinct and given in ascending order. Each value in the Back list is the index of a square marked “Go back to the start” and satisfies value ; the values are distinct and given in ascending order. No index appears in both the Lose list and the Back list.
The end of the input is indicated by a line containing four zeros separated by spaces.
Output
For each dataset, print on a single line the probability that the game succeeds within the given number of turns, rounded to exactly six digits after the decimal point (for example, using printf("%.6f")).