[C++] DP Solution - Memos
Status: Accepted (8ms Code & 7.8 MB usage) \n\nSimilar problem: Out of Boundary Path\n\nMy solution to the above problem with explaination. You can find it  her
**Status**: Accepted (8ms Code & 7.8 MB usage) \n\nSimilar problem: [Out of Boundary Path](https://leetcode.com/problems/out-of-boundary-paths)\n\nMy solution to the above problem with explaination. You can find it [ here.](https://leetcode.com/problems/out-of-boundary-paths/discuss/632968/hurrah-my-first-dp-accepted-solution-c)\n\nUpvote if it helps! Thanks.\n```\nclass Solution {\npublic:\n    double helperFunction(int N, int K, int r, int c, vector<vector<vector<double>>> & dp){\n        if(r < 0 || c < 0 || r >= N || c >= N) return 0;\n        if(K == 0) return 1;\n        if(dp[r][c][K] != -1) return dp[r][c][K];\n        double sum = helperFunction(N, K - 1, r - 2, c - 1, dp) +\n                     helperFunction(N, K - 1, r - 1, c - 2, dp) +\n                     helperFunction(N, K - 1, r + 1, c - 2, dp) +\n                     helperFunction(N, K - 1, r + 2, c - 1, dp) +\n                     helperFunction(N, K - 1, r - 2, c + 1, dp) +\n                     helperFunction(N, K - 1, r - 1, c + 2, dp) +\n                     helperFunction(N, K - 1, r + 1, c + 2, dp) +\n                     helperFunction(N, K - 1, r + 2, c + 1, dp);\n        sum = sum / 8;\n        dp[r][c][K] = sum;\n        return dp[r][c][K];\n    \n    }\n    double knightProbability(int N, int K, int r, int c) {\n        vector<vector<vector<double>>> dp(N + 1, vector<vector<double>> (N + 1, vector<double>(K + 1, -1)));\n        return helperFunction(N, K, r, c, dp);\n    }\n};\n```
['Dynamic Programming', 'Memoization', 'C', 'C++']