Problem
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Ā notcontinue moving to 4
.)
Return the least number of moves required to reach squareĀ N*N.Ā If it is not possible, returnĀ -1
.
Example 1:
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: BFS
Time complexity: O(n*n)
Space complexity: O(n*n)
C++
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 |
// Author: Huahua, 8 ms (beats 100%) class Solution { public: int snakesAndLadders(vector<vector<int>>& board) { const int N = board.size(); int steps = 0; vector<int> seen(N * N + 1, 0); queue<int> q; q.push(1); seen[1] = 1; while (!q.empty()) { int size = q.size(); ++steps; while (size--) { int s = q.front(); q.pop(); for (int x = s + 1; x <= min(s + 6, N * N); ++x) { int r, c; getRC(x, N, &r, &c); int nx = board[r][c] == -1 ? x : board[r][c]; if (seen[nx]) continue; if (nx == N * N) return steps; q.push(nx); seen[nx] = 1; } } } return -1; } private: void getRC(int s, int N, int* r, int* c) { int y = (s - 1) / N; int x = (s - 1) % N; *r = N - 1 - y; *c = (y % 2) ? N - 1 - x : x; } }; |