Knight Probability in Chessboard LeetCode solution in Python
1class Solution:2def knightProbability(self, n: int, k: int, row: int, column: int) -> float:34x_dir = [2, 1, -1, -2, -2, -1, 1, 2]5y_dir = [1, 2, 2, 1, -1, -2, -2, -1]67cache = {}89def kMoves(i, j, moves):10if i >= n or j >= n or i < 0 or j < 0:11return 01213if moves == k:14return 11516if (i, j, moves) in cache:17return cache[(i, j, moves)]1819totMoves = 020for ind in range(8):21totMoves += kMoves(i + x_dir[ind], j + y_dir[ind], moves + 1) * (1 / 8)2223cache[(i, j, moves)] = totMoves24return totMoves2526return kMoves(row, column, 0)