Cisco | SDE (OA) |Snakes & Ladders Pro
Anonymous User
3115

Snakes & Ladders Pro
Given a snake and ladder game of matrix n x n and dice of max value 'm'. The value of a given dice throw can be any number in the range [1,m]

The layout for a matrix of size 3 i.e. 3x3 is as follows:

7 8 9
6 5 4
1 2 3

Similarly the layout for a mtrix of size 4 i.e is as follows:

16 15 14 13
 9 10 11 12
 8  7  6  5
 1  2  3  4

Snakes and ladder moves are depicted as coordinates [a,b] i.e. move from a to b where a and b are coordinates in the matrix.

ladders:l1 -(2,6) 
        l2 - (8,11)
snakes: s1 - (9,3)
        s2 - (15,1)

If a<b, then its a ladder. Else is a>b, then its a snake.
The goal is to find the following
1. minimal number of dice rolls required to move from x to y.
2. No. of ladders taken in the path
3. No. of slides encountered in the path

Input Format:-
The first line is the size of matrix 'n'
The second line is the max value of the dice 'm'
The third line is the numbers of coordinates (snakes & ladders) say 'p'
For each coordinate say (a,b), a and b are captuered in 2 lines
Last 2 lines respresent the starting coordinate 'x' and end coordinate 'y'

Output format:-
Output consist of 3 Integers.

  1. Minimum number of dice rolls required to move from x to y
  2. No. of ladders taken
  3. No. of slides taken

Constraints :
0<n<=100
0<m<=100

Sample input 1:

4
2
4
2
6
8
11
9
3
15
1
1
7 

Sample output 1:

2
1
0

Sample explanation 1:
This is a 4x4 matrix (first line) with max value of the dice being 2 (second line). third line indicates there are 4 coordinates. Coordinates are (2,6), (8,11), (9,3) and (15,1) in the next 8 lines. 1st 2 coordinates are ladders and last 2 are snakes. Next line is starting coordinte i.e. 1 and last line is end coordinate i.e. 7.
Minimum rolls to move from 1 to 8 is 2 rolls. Start at 1. Throw 1 on 1st roll to land on 2. Use ladder to climb from 2 to 6. Throw 1 on 2nd roll to land on 7.
Hence 2 rolls, 1 ladders and 0 slides.

Comments (4)