Skip to content

Commit

Permalink
Add Java solution 909. Snakes and Ladders
Browse files Browse the repository at this point in the history
  • Loading branch information
gnohgnij committed Jan 24, 2023
1 parent cffabe4 commit ad2107f
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions java/0909-snakes-and-ladders.java
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--;
}
}
}

0 comments on commit ad2107f

Please sign in to comment.