-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path576. Out of Boundary Paths.java
42 lines (38 loc) · 1.53 KB
/
576. Out of Boundary Paths.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
private static final int MOD = 1000000007;
private long[][][] dp;
public int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
dp = new long[m][n][maxMove + 1];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k <= maxMove; k++) {
dp[i][j][k] = -1;
}
}
}
return (int) countPaths(startRow, startColumn, maxMove, m, n);
}
private long countPaths(int row, int col, int remainingMoves, int m, int n) {
// Base case: if out of bounds
if (row < 0 || row >= m || col < 0 || col >= n) {
return 1; // One valid path found
}
// Base case: no remaining moves
if (remainingMoves == 0) {
return 0; // No valid path found
}
// Return cached result if already calculated
if (dp[row][col][remainingMoves] != -1) {
return dp[row][col][remainingMoves];
}
long paths = 0;
// Explore all four directions
paths += countPaths(row + 1, col, remainingMoves - 1, m, n); // Down
paths += countPaths(row - 1, col, remainingMoves - 1, m, n); // Up
paths += countPaths(row, col + 1, remainingMoves - 1, m, n); // Right
paths += countPaths(row, col - 1, remainingMoves - 1, m, n); // Left
// Store the result in dp array and return
dp[row][col][remainingMoves] = paths % MOD;
return dp[row][col][remainingMoves];
}
}