|
41 | 41 |
|
42 | 42 | <!-- 这里可写通用的实现逻辑 -->
|
43 | 43 |
|
| 44 | +**方法一:记忆化搜索** |
| 45 | + |
| 46 | +定义 `dfs(i, j, k)` 表示当前位于坐标 $(i, j)$,且剩余移动次数为 $k$ 时,可以出界的路径数。记忆化搜索即可。 |
| 47 | + |
| 48 | +时间复杂度 $O(m\times n\times k)$,空间复杂度 $O(m\times n\times k)$。其中 $m$, $n$, $k$ 分别表示网格的行数、列数、最大可移动次数。 |
| 49 | + |
44 | 50 | <!-- tabs:start -->
|
45 | 51 |
|
46 | 52 | ### **Python3**
|
47 | 53 |
|
48 | 54 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
49 | 55 |
|
50 | 56 | ```python
|
51 |
| - |
| 57 | +class Solution: |
| 58 | + def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int: |
| 59 | + @cache |
| 60 | + def dfs(i, j, k): |
| 61 | + if i < 0 or j < 0 or i >= m or j >= n: |
| 62 | + return 1 |
| 63 | + if k <= 0: |
| 64 | + return 0 |
| 65 | + res = 0 |
| 66 | + for a, b in [[-1, 0], [1, 0], [0, 1], [0, -1]]: |
| 67 | + x, y = i + a, j + b |
| 68 | + res += dfs(x, y, k - 1) |
| 69 | + res %= mod |
| 70 | + return res |
| 71 | + |
| 72 | + mod = 10**9 + 7 |
| 73 | + return dfs(startRow, startColumn, maxMove) |
52 | 74 | ```
|
53 | 75 |
|
54 | 76 | ### **Java**
|
55 | 77 |
|
56 | 78 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
57 | 79 |
|
58 | 80 | ```java
|
| 81 | +class Solution { |
| 82 | + private int m; |
| 83 | + private int n; |
| 84 | + private int[][][] f; |
| 85 | + private static final int[] DIRS = {-1, 0, 1, 0, -1}; |
| 86 | + private static final int MOD = (int) 1e9 + 7; |
| 87 | + |
| 88 | + public int findPaths(int m, int n, int maxMove, int startRow, int startColumn) { |
| 89 | + this.m = m; |
| 90 | + this.n = n; |
| 91 | + f = new int[m + 1][n + 1][maxMove + 1]; |
| 92 | + for (var a : f) { |
| 93 | + for (var b : a) { |
| 94 | + Arrays.fill(b, -1); |
| 95 | + } |
| 96 | + } |
| 97 | + return dfs(startRow, startColumn, maxMove); |
| 98 | + } |
| 99 | + |
| 100 | + private int dfs(int i, int j, int k) { |
| 101 | + if (i < 0 || i >= m || j < 0 || j >= n) { |
| 102 | + return 1; |
| 103 | + } |
| 104 | + if (f[i][j][k] != -1) { |
| 105 | + return f[i][j][k]; |
| 106 | + } |
| 107 | + if (k == 0) { |
| 108 | + return 0; |
| 109 | + } |
| 110 | + int res = 0; |
| 111 | + for (int t = 0; t < 4; ++t) { |
| 112 | + int x = i + DIRS[t]; |
| 113 | + int y = j + DIRS[t + 1]; |
| 114 | + res += dfs(x, y, k - 1); |
| 115 | + res %= MOD; |
| 116 | + } |
| 117 | + f[i][j][k] = res; |
| 118 | + return res; |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +```java |
| 124 | +class Solution { |
| 125 | + public int findPaths(int m, int n, int N, int i, int j) { |
| 126 | + final int MOD = (int) (1e9 + 7); |
| 127 | + final int[] dirs = new int[] {-1, 0, 1, 0, -1}; |
| 128 | + int[][] f = new int[m][n]; |
| 129 | + f[i][j] = 1; |
| 130 | + int res = 0; |
| 131 | + for (int step = 0; step < N; ++step) { |
| 132 | + int[][] temp = new int[m][n]; |
| 133 | + for (int x = 0; x < m; ++x) { |
| 134 | + for (int y = 0; y < n; ++y) { |
| 135 | + for (int k = 0; k < 4; ++k) { |
| 136 | + int tx = x + dirs[k], ty = y + dirs[k + 1]; |
| 137 | + if (tx >= 0 && tx < m && ty >= 0 && ty < n) { |
| 138 | + temp[tx][ty] += f[x][y]; |
| 139 | + temp[tx][ty] %= MOD; |
| 140 | + } else { |
| 141 | + res += f[x][y]; |
| 142 | + res %= MOD; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + f = temp; |
| 148 | + } |
| 149 | + return res; |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +### **C++** |
| 155 | + |
| 156 | +```cpp |
| 157 | +class Solution { |
| 158 | +public: |
| 159 | + int m; |
| 160 | + int n; |
| 161 | + const int mod = 1e9 + 7; |
| 162 | + int f[51][51][51]; |
| 163 | + int dirs[5] = {-1, 0, 1, 0, -1}; |
| 164 | + |
| 165 | + int findPaths(int m, int n, int maxMove, int startRow, int startColumn) { |
| 166 | + memset(f, 0xff, sizeof(f)); |
| 167 | + this->m = m; |
| 168 | + this->n = n; |
| 169 | + return dfs(startRow, startColumn, maxMove); |
| 170 | + } |
| 171 | + |
| 172 | + int dfs(int i, int j, int k) { |
| 173 | + if (i < 0 || i >= m || j < 0 || j >= n) return 1; |
| 174 | + if (f[i][j][k] != -1) return f[i][j][k]; |
| 175 | + if (k == 0) return 0; |
| 176 | + int res = 0; |
| 177 | + for (int t = 0; t < 4; ++t) { |
| 178 | + int x = i + dirs[t], y = j + dirs[t + 1]; |
| 179 | + res += dfs(x, y, k - 1); |
| 180 | + res %= mod; |
| 181 | + } |
| 182 | + f[i][j][k] = res; |
| 183 | + return res; |
| 184 | + } |
| 185 | +}; |
| 186 | +``` |
59 | 187 |
|
| 188 | +### **Go** |
| 189 | +
|
| 190 | +```go |
| 191 | +func findPaths(m int, n int, maxMove int, startRow int, startColumn int) int { |
| 192 | + f := make([][][]int, m+1) |
| 193 | + for i := range f { |
| 194 | + f[i] = make([][]int, n+1) |
| 195 | + for j := range f[i] { |
| 196 | + f[i][j] = make([]int, maxMove+1) |
| 197 | + for k := range f[i][j] { |
| 198 | + f[i][j][k] = -1 |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + var mod int = 1e9 + 7 |
| 203 | + dirs := []int{-1, 0, 1, 0, -1} |
| 204 | + var dfs func(i, j, k int) int |
| 205 | + dfs = func(i, j, k int) int { |
| 206 | + if i < 0 || i >= m || j < 0 || j >= n { |
| 207 | + return 1 |
| 208 | + } |
| 209 | + if f[i][j][k] != -1 { |
| 210 | + return f[i][j][k] |
| 211 | + } |
| 212 | + if k == 0 { |
| 213 | + return 0 |
| 214 | + } |
| 215 | + res := 0 |
| 216 | + for t := 0; t < 4; t++ { |
| 217 | + x, y := i+dirs[t], j+dirs[t+1] |
| 218 | + res += dfs(x, y, k-1) |
| 219 | + res %= mod |
| 220 | + } |
| 221 | + f[i][j][k] = res |
| 222 | + return res |
| 223 | + } |
| 224 | + return dfs(startRow, startColumn, maxMove) |
| 225 | +} |
60 | 226 | ```
|
61 | 227 |
|
62 | 228 | ### **...**
|
|
0 commit comments