Skip to content

Commit 1167ee6

Browse files
Merge pull request #1838 from CodingTestStudy2/염혜정
[염혜정] Day17
2 parents b874ff6 + d554d6c commit 1167ee6

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// bfs + dp
2+
3+
class Solution {
4+
public int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
5+
final int MOD = 1_000_000_007;
6+
long[][] dp = new long[m][n];
7+
dp[startRow][startColumn] = 1;
8+
int count = 0;
9+
int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
10+
11+
for (int move = 0; move < maxMove; move++) {
12+
long[][] next = new long[m][n];
13+
for (int i = 0; i < m; i++) {
14+
for (int j = 0; j < n; j++) {
15+
if (dp[i][j] == 0) continue;
16+
for (int[] d : dirs) {
17+
int ni = i + d[0];
18+
int nj = j + d[1];
19+
if (ni < 0 || ni >= m || nj < 0 || nj >= n) {
20+
count = (int) ((count + dp[i][j]) % MOD);
21+
} else {
22+
next[ni][nj] = (next[ni][nj] + dp[i][j]) % MOD;
23+
}
24+
}
25+
}
26+
}
27+
dp = next;
28+
}
29+
30+
return count;
31+
}
32+
}

0 commit comments

Comments
 (0)