Tabulation and memo solutions explained.
Intuition: \ncheck which moves are going outside, take them as base case and return 1. and every time we have 8 ways to move so store the no. of 1s divide by 8.
# Intuition: \ncheck which moves are going outside, take them as base case and return 1. and every time we have 8 ways to move so store the no. of 1s divide by 8.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach:\nTake a 3d dp and write a function containing base condition which return 1 when it is inside of chess board and return 0 when it is outside of chess board, then calculate total ways from 8 ways which return 1, then divide by 8.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: see the solution\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: see the solution\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n//Memoization \nTime complexity: O(n*n*k)\nSpace complexity: O(n*n*k) + O(n*k)\n\nclass Solution {\npublic:\n    \n    vector<pair<int, int>>moves={{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-1, -2}, {-2, -1}, {2, -1}, {1, -2}};\n    double solve(int row, int col, int n, int k,  vector<vector<vector<double>>> &dp){\n         if(row<0 || col<0 || row>=n || col>=n) return 0.0;\n    if(k==0) {\n     return 1.0;\n    }\n    if(dp[k][row][col]!=-1) return dp[k][row][col];\n    double ans=0;\n    for(auto it:moves){\n        int nrow=row+it.first;\n        int ncol=col+it.second;\n        ans=ans+solve(nrow, ncol, n, k-1, dp);\n    }\n    return dp[k][row][col]=ans/8;\n    }\n    double knightProbability(int n, int k, int row, int column) {\n        vector<vector<vector<double>>>dp(k+1, vector<vector<double>>(n, vector<double>(n, -1)));\n        return solve(row, column, n, k, dp);\n    }\n};\n\n//Tabulation approach \nTime complexity: O(n*n*k)\nSpace complexity: O(n*n*k)\n\nclass Solution {\npublic:\n    vector<pair<int, int>> moves = {{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-1, -2}, {-2, -1}, {2, -1}, {1, -2}};\n    \n    double knightProbability(int n, int k, int row, int column) {\n        vector<vector<vector<double>>> dp(k + 1, vector<vector<double>>(n, vector<double>(n, 0.0)));\n    \n        for (int i = 0; i < n; ++i) {\n            for (int j = 0; j < n; ++j) {\n                dp[0][i][j] = 1.0;\n            }\n        }\n        \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                    double ans = 0.0;\n                    for (auto it : moves) {\n                        int nrow = i + it.first;\n                        int ncol = j + it.second;\n                        if (nrow >= 0 && ncol >= 0 && nrow < n && ncol < n) {\n                            ans += dp[m - 1][nrow][ncol];\n                        }\n                    }\n                    dp[m][i][j] = ans / 8.0;\n                }\n            }\n        }\n        \n        return dp[k][row][column];\n    }\n};\n\n```
['C++']