Video Solution | Java C++ Python
\n\n\n\npublic class Solution {\n    public double knightProbability(int n, int k, int row, int column) {\n        int[][] dir = {{1,2},{-1,2},{1,-2},{-1,-2},{2
<iframe width="560" height="315" src="https://www.youtube.com/embed/7HaNCeT0nOw" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>\n\n\n```\npublic class Solution {\n    public double knightProbability(int n, int k, int row, int column) {\n        int[][] dir = {{1,2},{-1,2},{1,-2},{-1,-2},{2,1},{-2,1},{2,-1},{-2,-1}};\n\n        double[][][] dp = new double[k + 1][n][n];\n        dp[0][row][column] = 1.0;\n        int prevI;\n        int prevJ;\n        for (int m = 1; m <= k; m++) {\n            for (int i = 0; i < n; i++) {\n                for (int j = 0; j < n; j++) { \n                    for (int[] d : dir) {\n                        prevI = i - d[0];\n                        prevJ = j - d[1];\n                        if (prevI >= 0 && prevI < n && prevJ >= 0 && prevJ < n) {\n                            dp[m][i][j] += dp[m - 1][prevI][prevJ] / 8.0;\n                        }\n                    }\n                }\n            }\n        }\n\n        double ans = 0.0;\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < n; j++) {\n                ans += dp[k][i][j];\n            }\n        }\n        return ans;\n    }\n}\n```\n```\nclass Solution {\npublic:\n    double knightProbability(int n, int k, int row, int column) {\n        vector<vector<int>> dir = {{1, 2}, {-1, 2}, {1, -2}, {-1, -2}, {2, 1}, {-2, 1}, {2, -1}, {-2, -1}};\n\n        vector<vector<vector<double>>> dp(k + 1, vector<vector<double>>(n, vector<double>(n, 0.0)));\n        dp[0][row][column] = 1.0;\n\n        int prevI, prevJ;\n        for (int m = 1; m <= k; m++) {\n            for (int i = 0; i < n; i++) {\n                for (int j = 0; j < n; j++) {\n                    for (const auto& d : dir) {\n                        prevI = i - d[0];\n                        prevJ = j - d[1];\n                        if (prevI >= 0 && prevI < n && prevJ >= 0 && prevJ < n) {\n                            dp[m][i][j] += dp[m - 1][prevI][prevJ] / 8.0;\n                        }\n                    }\n                }\n            }\n        }\n\n        double ans = 0.0;\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < n; j++) {\n                ans += dp[k][i][j];\n            }\n        }\n\n        return ans;\n    }\n};\n```\n\n```\nclass Solution:\n    def knightProbability(self, n: int, k: int, row: int, column: int) -> float:\n        dir = [[1, 2], [-1, 2], [1, -2], [-1, -2], [2, 1], [-2, 1], [2, -1], [-2, -1]]\n\n        dp = [[[0.0] * n for _ in range(n)] for _ in range(k + 1)]\n        dp[0][row][column] = 1.0\n\n        for m in range(1, k + 1):\n            for i in range(n):\n                for j in range(n):\n                    for d in dir:\n                        prevI, prevJ = i - d[0], j - d[1]\n                        if 0 <= prevI < n and 0 <= prevJ < n:\n                            dp[m][i][j] += dp[m - 1][prevI][prevJ] / 8.0\n\n        ans = sum(dp[k][i][j] for i in range(n) for j in range(n))\n        return ans\n\n```\n\n
['C', 'Python', 'Java']