Problem Statement:

You are given an n x n integer matrix board where the cells are labeled from 1 to n2 in a Boustrophedon style starting from the bottom left of the board (i.e. board[n – 1][0]) and alternating directions in each row.
You start on square 1 of the board. In each move, starting from square curr, do the following:

  • Choose a destination square next with a label in the range [curr + 1, min(curr + 6, n2)].
  • This choice simulates the result of a standard 6-sided die roll: i.e., there are always at most 6 destinations, regardless of the size of the board.
  • If next has a snake or ladder, you must move to the destination of that snake or ladder. Otherwise, you move to next.

The game ends when you reach square n2.
A board square on row r and column c has a snake or ladder if board[r] != -1. The destination of that snake or ladder is board[r]. Squares 1 and n2 do not have a snake or ladder.
Note that you only take a snake or ladder at most once per move. If the destination of a snake or ladder is the start of another snake or ladder, you do not follow the subsequent snake or ladder.

  • For example, suppose the board is [[-1,4],[-1,3]], and on the first move, your destination square is 2. You follow the ladder to square 3, but do not follow the subsequent ladder to 4.

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

Example 1:

Input: board = [[-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: 
In 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 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.
This is the lowest possible number of moves to reach the last square, so return 4.

Example 2:

Input: board = [[-1,-1],[-1,3]]
Output: 1

Constraints:

  • n == board.length == board[i].length
  • 2 <= n <= 20
  • grid[i][j] is either -1 or in the range [1, n2].
  • The squares labeled 1 and n2 do not have any ladders or snakes.

CODE

import java.util.*;

class Innoskrit {
    public int snakesAndLadders(int[][] board) {
        int minSteps = 0;
        int n = board.length;
        int start = 1, end = n * n;
        boolean[][] visited = new boolean[n][n];
        Queue<Integer> queue = new LinkedList<>();

        visited[n - 1][0] = true;
        queue.add(start);

        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                int currentPosition = queue.poll();
                if (currentPosition == end) return minSteps;
                // Roll the dice 6 times and add the possible destinations to the Queue.
                for (int dice = 1; dice <= 6; dice++) {
                    if (dice + currentPosition > end) 
                        break;
                    int[] nextPosition = findCoordinates(currentPosition + dice, n);
                    int nextRow = nextPosition[0], nextColumn = nextPosition[1];
                    if (!visited[nextRow][nextColumn]) {
                        visited[nextRow][nextColumn] = true;
                        if (board[nextRow][nextColumn] != -1) {
                            queue.add(board[nextRow][nextColumn]);
                        }
                        else {
                            queue.add(currentPosition + dice);
                        }
                    }
                }
            }
            minSteps++;
        }
        return -1;
    }
    
    private int[] findCoordinates(int currentPosition, int n) {
        int row = n - (currentPosition - 1) / n - 1;
        int col = (currentPosition - 1) % n;
        if (row % 2 == n % 2) return new int[]{row, n - 1 - col};
        else return new int[]{row, col};
    }
    
    public static void main (String[] args) {
        Innoskrit obj = new Innoskrit();
        int board[][] = new int[][]{
            {-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}};
        System.out.println(obj.snakesAndLadders(board));
    }
}
#include <bits/stdc++.h>
using namespace std;

class Innoskrit {
public:
    int snakesAndLadders(vector<vector<int>>& board) {
        int minSteps = 0;
        int n = board.size();
        int start = 1, end = n * n;
        vector<vector<bool>> visited (n, vector<bool> (n, false));
        queue<int> q;

        visited[n - 1][0] = true;
        q.push(start);

        while (!q.empty()) {
            int size = q.size();
            for (int i = 0; i < size; i++) {
                int currentPosition = q.front();
                q.pop();
                if (currentPosition == end) return minSteps;
                // Roll the dice 6 times and add the possible destinations to the Queue.
                for (int dice = 1; dice <= 6; dice++) {
                    if (dice + currentPosition > end) 
                        break;
                    pair<int, int> nextPosition = findCoordinates(currentPosition + dice, n);
                    int nextRow = nextPosition.first, nextColumn = nextPosition.second;
                    if (!visited[nextRow][nextColumn]) {
                        visited[nextRow][nextColumn] = true;
                        if (board[nextRow][nextColumn] != -1) {
                            q.push(board[nextRow][nextColumn]);
                        }
                        else {
                            q.push(currentPosition + dice);
                        }
                    }
                }
            }
            minSteps++;
        }
        return -1;
    }
    
    pair<int, int> findCoordinates(int currentPosition, int n) {
        int row = n - (currentPosition - 1) / n - 1;
        int col = (currentPosition - 1) % n;
        if (row % 2 == n % 2) return {row, n - 1 - col};
        else return {row, col};
    }
};

int main(int argc, char * argv[]) {
    Innoskrit obj;
    vector<vector<int>> board {
        {-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}};
    cout << obj.snakesAndLadders(board) << endl;
    return 0;
}
from collections import deque

class Innoskrit:
    
    def find_coordinates(self, current_position, n):
        row = n - 1 - (current_position - 1) // n
        col = (current_position - 1) % n
        if row % 2 == n % 2:
            return (row, n - 1 - col)
        else:
            return (row, col)
    
    def snakesAndLadders(self, board):
        min_steps = 0
        n = len(board)
        start, end = 1, n * n
        
        visited = [[False for i in range(n)] for j in range(n)]
        queue = deque()
        
        visited[n - 1][0] = True
        queue.append(start)
        
        while queue:
            size = len(queue)
            for i in range(size):
                current_position = queue.popleft()
                
                if current_position == end:
                    return min_steps
                
                for dice in range(1, 7):
                    if current_position + dice > end:
                        break
                    next_row, next_col = self.find_coordinates(current_position + dice, n)
                    if not visited[next_row][next_col]:
                        visited[next_row][next_col] = True
                        if board[next_row][next_col] != -1:
                            queue.append(board[next_row][next_col])
                        else:
                            queue.append(current_position + dice)
            min_steps += 1   
            
        print(visited)
        return -1
                    
        
if __name__ == '__main__':
    obj = Innoskrit()
    board = [
        [-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]]
    print(obj.snakesAndLadders(board))

Output:

4