On an n x n chessboard, a knight starts at the cell (row, column) and attempts to make exactly k moves. The rows and columns are 0-indexed, so the top-left cell is (0, 0), and the bottom-right cell is (n - 1, n - 1).

A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.

Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.

The knight continues moving until it has made exactly k moves or has moved off the chessboard.

Return the probability that the knight remains on the board after it has stopped moving.

&nbsp;
Example 1:

Input: n = 3, k = 2, row = 0, column = 0
Output: 0.06250
Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
From each of those positions, there are also two moves that will keep the knight on the board.
The total probability the knight stays on the board is 0.0625.


Example 2:

Input: n = 1, k = 0, row = 0, column = 0
Output: 1.00000


&nbsp;
Constraints:


	1 &lt;= n &lt;= 25
	0 &lt;= k &lt;= 100
	0 &lt;= row, column &lt;= n


class Solution:
    def knightProbability(self, n: int, k: int, row: int, column: int) -> float:
        
        x_dir = [2, 1, -1, -2, -2, -1, 1, 2]
        y_dir = [1, 2, 2, 1, -1, -2, -2, -1]
        
        cache = {}
        
        def kMoves(i, j, moves):
            if i >= n or j >= n or i < 0 or j < 0:
                return 0
            
            if moves == k:
                return 1
            
            if (i, j, moves) in cache:
                return cache[(i, j, moves)]
            
            totMoves = 0
            for ind in range(8):
                totMoves += kMoves(i+x_dir[ind], j+y_dir[ind], moves+1)*(1/8)
            
            cache[(i, j, moves)] = totMoves
            return totMoves
        
        return kMoves(row, column, 0)
class Solution {
    public double knightProbability(int n, int k, int row, int column) {
        double [][]curr=new double[n][n];
        double [][]next=new double[n][n];
        
        curr[row][column]=1;
        
        int [][]dir={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
        for(int p=1;p<=k;p++){
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    if(curr[i][j]!=0){
                        
                        for(int d=0;d<8;d++){
                            int ni=i+dir[d][0];
                            int nj=j+dir[d][1];
                            
                            if(ni<0 || nj<0 || ni>=n || nj>=n){
                                continue;
                            }
                            
                            next[ni][nj]+=curr[i][j]/8.0;
                        }
                    }
                }
            }
            
            curr=next;
            next=new double[n][n];
        }
        
        double sum=0.0;
        
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                sum+=curr[i][j];
            }
        }
        
        return sum;
    }
}
class Solution {
public:
    
    // declare a dp
    
    double dp[30][30][105];
    
    // x and y co-ordinates of 8 directions
   
    vector<int> dx = {-2, -2, -1, 1, 2, 2, 1, -1};
    
    vector<int> dy = {-1, 1, 2, 2, 1, -1, -2, -2};
    
    double dfs(int i, int j, int n, int moves)
    {
        // base case if we have reached out of grid
        
        if(i < 0 || i >= n || j < 0 || j >= n)
            return 0;
        
        // if no moves are remaining
        
        if(moves <= 0)
            return 1;
        
        // if already calculated
        
        if(dp[i][j][moves] != 0)
            return dp[i][j][moves];
        
        // find total possible ways of staying on chess board
        
        double ans = 0;
        
        for(int k = 0; k < 8; k++)
        {
            int new_row = i + dx[k];
            
            int new_col = j + dy[k];
            
            ans += dfs(new_row, new_col, n, moves - 1);
        }
        
        // for each cell there are 8 possible moves, so probablity will be no. of successfull moves / 8
        
        // store the result and return
        
        return dp[i][j][moves] = ans / 8.0;
    }
    
    double knightProbability(int n, int k, int row, int column) {
        
        // initialize the dp with 0
        
        memset(dp, 0, sizeof(dp));
       
        return dfs(row, column, n, k);
    }
};
var knightProbability = function(n, k, row, column) {
    if (k === 0) return 1;
    const dirs = [[-2, -1], [-1, -2], [1, -2], [2, -1], [2, 1], [1, 2], [-1, 2], [-2, 1]];
    const dp = Array(k + 1)
        .fill('')
        .map(_ => Array(n).fill('').map(_ => Array(n).fill(0)));

    const isOut = (pos) => pos < 0 || pos > n - 1;
    const move = (x = row, y = column, step = k) => {
        if (step === 0) return 1;
        const moves = dp[step][x][y];
        if (moves !== 0) return moves;

        for (const [mvoeX, moveY] of dirs) {
            const nextX = x + mvoeX;
            const nextY = y + moveY;
            if (isOut(nextX) || isOut(nextY)) continue;

            dp[step][x][y] += move(nextX, nextY, step - 1);
        }
        return dp[step][x][y];
    };

    move();
    return dp[k][row][column] / 8 ** k;
};
Knight Probability in Chessboard