forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0909-snakes-and-ladders.java
52 lines (46 loc) · 1.36 KB
/
0909-snakes-and-ladders.java
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
52
class Solution {
public int snakesAndLadders(int[][] board) {
int n = board.length;
reverseBoard(board);
boolean[] visited = new boolean[n*n+1];
Queue<int[]> q = new LinkedList<>();
q.offer(new int[]{1, 0});
visited[1] = true;
while(!q.isEmpty()) {
int[] curr = q.poll();
for(int j=1; j<=6; j++) {
int next = curr[0] + j;
int[] coor = squareToCoor(next, n);
if(board[coor[0]][coor[1]] != -1) {
next = board[coor[0]][coor[1]];
}
if(next == n*n) {
return curr[1] + 1;
}
if(!visited[next]) {
visited[next] = true;
q.offer(new int[]{next, curr[1] + 1});
}
}
}
return -1;
}
public int[] squareToCoor(int square, int n) {
int row = (square - 1) / n;
int col = (square - 1) % n;
if(row % 2 != 0) {
col = n - 1 - col;
}
return new int[]{row, col};
}
public void reverseBoard(int[][] board) {
int l = 0, r = board.length-1;
while(l < r) {
int[] temp = board[l];
board[l] = board[r];
board[r] = temp;
l++;
r--;
}
}
}