Problem description:

On an N x N board, the numbers from 1 to N*N are written boustrophedonically starting from the bottom left of the board, and alternating direction each row. For example, for a 6 x 6 board, the numbers are written as follows:

You start on square 1 of the board (which is always in the last row and first column). Each move, starting from square x, consists of the following:

You choose a destination square S with number x+1, x+2, x+3, x+4, x+5, or x+6, provided this number is <= N*N.
(This choice simulates the result of a standard 6-sided die roll: ie., there are always at most 6 destinations.)
If S has a snake or ladder, you move to the destination of that snake or ladder. Otherwise, you move to S.
A board square on row r and column c has a “snake or ladder” if board[r][c] != -1. The destination of that snake or ladder is board[r][c].

Note that you only take a snake or ladder at most once per move: if the destination to a snake or ladder is the start of another snake or ladder, you do not continue moving. (For example, if the board is [[4,-1],[-1,3]], and on the first move your destination square is 2, then you finish your first move at 3, because you do not continue moving to 4.)

Return the least number of moves required to reach square N*N. If it is not possible, return -1.

Example 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input: [
[-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1],
[-1,35,-1,-1,13,-1],
[-1,-1,-1,-1,-1,-1],
[-1,15,-1,-1,-1,-1]]
Output: 4
Explanation:
At the beginning, you start at square 1 [at row 5, column 0].
You decide to move to square 2, and must take the ladder to square 15.
You then decide to move to square 17 (row 3, column 5), and must take the snake to square 13.
You then decide to move to square 14, and must take the ladder to square 35.
You then decide to move to square 36, ending the game.
It can be shown that you need at least 4 moves to reach the N\*N-th square, so the answer is 4.

Note:

2 <= board.length = board[0].length <= 20
board[i][j] is between 1 and N*N or is equal to -1.
The board square with number 1 has no snake or ladder.
The board square with number N*N has no snake or ladder.

Solution:

The main idea of this question is using BFS to find out how many BFS iteration we need for reaching the goal.

  1. Calculate the newRow and newCol. Because it starts from left bottom side and will change direction, so we can use two functions to do the math.
  2. For each iteration of BFS, we should do the following things. First, check if we reach the goal n*n. Second, find out if there’s a bridge or snake in the next slot, then move.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution {
public:
int snakesAndLadders(vector<vector<int>>& board) {
//BFS
//use a variable step to calculate how many steps
//for each iteration, find out if the next step can reach to the end
//if can not reach to the end, then add i= 1~6 to current pos
if(board.empty()) return 0;
int step= 0;
int n= board.size();
queue<int> q;
vector<bool> visited(n*n, false);
q.push(1);
visited[0]= true;

while(!q.empty()){
step++;
int size= q.size();
for(int i= 0; i< size; i++){
int pos= q.front(); q.pop();
for(int i= 1; i<= 6; i++){
int next= pos+ i;
if(next == n*n)
return step;

int nextRow= getRow(next, n);
int nextCol= getCol(next, n);

if(board[nextRow][nextCol] != -1){
next= board[nextRow][nextCol];
if(next == n*n)
return step;
nextRow= getRow(next, n);
nextCol= getCol(next, n);
}
if(!visited[next-1]){
q.push(next);
visited[next-1]= true;
}
}
}
}
return -1;
}
int getRow(int v, int n){
return n-1-(v-1)/n;
}
int getCol(int v, int n){
return (((v-1)/n) % 2 == 0) ? ((v-1)%n) : (n-1-(v-1)%n);
}
};

time complexity: $O(n^2)$
space complexity: $O(n^2)$
reference:
https://goo.gl/Zbc7yK