PCPC12F - Snakes and Ladders Again


Snakes and Ladders (or Chutes and Ladders) is an ancient Indian board game regarded today as a worldwide classic. It is played between two or more players on a game board having numbered squares (fields) on a grid. A number of "ladders" and "snakes" (or "chutes") are pictured on the board, each connecting two specific board squares. The object of the game is to navigate one's game piece from the start (Bottom square) to the finish (Top Square), helped or hindered by ladders and snakes, respectively. The historic version had root in morality lessons, where a player's progression up the board represented a life journey complicated by virtues (ladders) and vices (snakes).  If, after throwing a dice, a player's token lands on the lower-numbered end of a "ladder", the player moves his token up to the ladder's higher-numbered square. If he lands on the higher-numbered square of a "snake" (or chute), he must move his token down to the snake's lower-numbered square. If any of those cases takes places, we will call a square unstable. Otherwise it is stable.

The game is a simple race contest lacking a skill component, and is popular with young children.

In this problem you’re required to calculate the expected number of 6-sided die throws to move your game piece from the start (bottom square) to the finish (top square).

Formal game description

Fields are arranged on an NxM grid and numbered from 1 to N*M. Last field, indicated by N*M, is referred to as Top Square. Each player starts with a token on a square at position "0" (the imaginary space beside the “1” grid field; Bottom Square), which is always stable. So in total we have N*M+1 fields. In every turn player throws the die and moves up by the given number of squares. If that would result in a field higher than Top Square, then token is not moved. If the square that token ends on is unstable, it is moved as indicated by ladder or snake. This is repeated until token is placed a stable field. You can assume that a stable field can be reached from any field on the board. If this final, stable field is the Top Square, game ends and player wins.

PIC

Input

Input contains multiple test cases First line of each test case contains integers N, M, S, L. where n and m are the board dimensions, N (0 < N <= 10), M (0 < M <= 10), and S and L are number of snakes and ladders respectively. Next S lines describes snakes. Each line contains two integers: h and t, where h is the snake’s head position and t is the snake tail position. (0 < t < h <=N*M), Next L lines describes ladders. Each line contains two integers: p and q where p is the ladder’s bottom and q is the ladder’s top (0 < p < q < N*M).

The input will be terminated by the end of file.

NOTE! There could be more snakes and/or ladders leading from a single field. In such a case use the last snake/ladder specified in the input.

Output

Print one number per test case (each in separate line), expected number of dice throws needed to reach the Top Square. It's guaranteed that the Top is always reachable. Your round the result to exactly 3 decimal places.

Sample

Input
5 10 3 5
16 6
47 26
49 11
1 38
4 14
9 31
40 42
36 44

Output
30.198

Before you solve this you may want to try: Snakes and Ladders


hide comments
Mario Kostelac: 2014-04-29 21:57:14

Please, add test case number parameter. I've lost several hours because I didn't see that there are multiple tests per run :).

bojan: 2014-04-16 17:00:45

Last edit: 2014-04-16 17:01:15
Mitch Schwartz: 2014-04-03 16:34:05

@Bharath Reddy: http://en.wikipedia.org/wiki/Expected_value

Bharath Reddy: 2014-04-03 11:44:43

Last edit: 2014-04-18 06:56:31
Bojan Rosko: 2014-04-01 20:42:52

shouldn't it be h<N*M and q<=N*M ?

Shafaet: 2013-08-05 06:37:11

@Thomas Pribitzer: I dont see any problem in description. Just read until EOF and print output for each case.

abdelkarim: 2013-03-25 08:41:06

i/o files were fixed !
@:D thanks for discovering the bug .
@all i'm so sorry for this trouble !

Last edit: 2013-03-26 00:23:01

Added by:abdelkarim
Date:2012-12-28
Time limit:0.409s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:The First Palestinian Collegiate Programming Contest