Skip to content

Commit

Permalink
Sync LeetCode submission - Out of Boundary Paths (cpp)
Browse files Browse the repository at this point in the history
  • Loading branch information
pradeeptosarkar committed Jan 26, 2024
1 parent 95b025d commit a685597
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions LeetCode/problems/out_of_boundary_paths/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ class Solution {
public:
int d[4][2]= {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int dp[50][50][51];
int backtrack(int m, int n, int maxMove, int row, int col){
if (row < 0 or col < 0 or row==m or col==n) return 1;
int backtrack(int m, int n, int maxMove, int row, int col)
{
if (row < 0 or col < 0 or row==m or col==n)
return 1;

if (maxMove==0) return 0;
if (dp[row][col][maxMove]!=-1) return dp[row][col][maxMove];

if (dp[row][col][maxMove]!=-1)
return dp[row][col][maxMove];

int ans=0;
for (int k=0; k<4; k++) {

for (int k=0; k<4; k++)
{
int dx= row + d[k][0];
int dy= col+ d[k][1];
ans = (ans+ backtrack(m, n, maxMove-1, dx, dy))%1000000007;

}
return dp[row][col][maxMove]=ans;


return dp[row][col][maxMove]=ans;
}
int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {

int findPaths(int m, int n, int maxMove, int startRow, int startColumn)
{
memset(dp, -1, sizeof(dp));
return backtrack( m, n, maxMove, startRow, startColumn)%1000000007;
}
Expand Down

0 comments on commit a685597

Please sign in to comment.