forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Java solution 909. Snakes and Ladders
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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--; | ||
} | ||
} | ||
} |