Skip to content

Commit 286bdbd

Browse files
Create 0576-out-of-boundary-paths.java
1 parent 10a88dc commit 286bdbd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

java/0576-out-of-boundary-paths.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
int Rows, Cols;
3+
int MOD = (int) 1e9 + 7;
4+
public int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
5+
Rows = m;
6+
Cols = n;
7+
Map<String, Integer> cache = new HashMap<>();
8+
return dfs(startRow, startColumn, maxMove, cache);
9+
}
10+
private int dfs(int r, int c, int moves, Map<String, Integer> cache){
11+
if(r < 0 || r >= Rows || c < 0 || c >= Cols)
12+
return 1;
13+
if(moves == 0)
14+
return 0;
15+
String pos = r + "," + c + "," + moves;
16+
if(cache.containsKey(pos))
17+
return cache.get(pos);
18+
19+
int res = ((dfs(r-1, c, moves-1, cache) + dfs(r+1, c, moves-1, cache))%MOD + (dfs(r, c-1, moves-1, cache) + dfs(r, c+1, moves-1, cache))%MOD)%MOD;
20+
cache.put(pos, res);
21+
return res;
22+
}
23+
}

0 commit comments

Comments
 (0)