@@ -69,32 +69,294 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3565.Se
69
69
70
70
<!-- solution:start -->
71
71
72
- ### 方法一
72
+ ### 方法一:状态压缩 + DFS
73
+
74
+ 我们注意到,矩阵的大小不超过 $6 \times 6$,因此可以使用状态压缩来表示已经访问过的格子。我们可以使用一个整数 $\textit{st}$ 来表示已经访问过的格子,其中第 $i$ 位为 1 表示格子 $i$ 已经被访问过,0 表示未被访问过。
75
+
76
+ 接下来,我们遍历每一个格子作为起点,如果该格子是 0 或 1,则从该格子开始进行深度优先搜索(DFS)。在 DFS 中,我们将当前格子加入路径中,并将其标记为已访问。然后,我们检查当前格子的值,如果等于 $v$,则将 $v$ 加 1。接着,我们尝试向四个方向移动到相邻的格子,如果相邻格子未被访问且其值为 0 或 $v$,则继续进行 DFS。
77
+
78
+ 如果 DFS 成功找到了一条完整的路径,则返回该路径。如果无法找到完整路径,则回溯,撤销当前格子的访问标记,并尝试其他方向。
79
+
80
+ 时间复杂度 $O(m^2 \times n^2)$,空间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
73
81
74
82
<!-- tabs:start -->
75
83
76
84
#### Python3
77
85
78
86
``` python
79
-
87
+ class Solution :
88
+ def findPath (self , grid : List[List[int ]], k : int ) -> List[List[int ]]:
89
+ def f (i : int , j : int ) -> int :
90
+ return i * n + j
91
+
92
+ def dfs (i : int , j : int , v : int ):
93
+ nonlocal st
94
+ path.append([i, j])
95
+ if len (path) == m * n:
96
+ return True
97
+ st |= 1 << f(i, j)
98
+ if grid[i][j] == v:
99
+ v += 1
100
+ for a, b in pairwise(dirs):
101
+ x, y = i + a, j + b
102
+ if (
103
+ 0 <= x < m
104
+ and 0 <= y < n
105
+ and (st & 1 << f(x, y)) == 0
106
+ and grid[x][y] in (0 , v)
107
+ ):
108
+ if dfs(x, y, v):
109
+ return True
110
+ path.pop()
111
+ st ^= 1 << f(i, j)
112
+ return False
113
+
114
+ m, n = len (grid), len (grid[0 ])
115
+ st = 0
116
+ path = []
117
+ dirs = (- 1 , 0 , 1 , 0 , - 1 )
118
+ for i in range (m):
119
+ for j in range (n):
120
+ if grid[i][j] in (0 , 1 ):
121
+ if dfs(i, j, 1 ):
122
+ return path
123
+ path.clear()
124
+ st = 0
125
+ return []
80
126
```
81
127
82
128
#### Java
83
129
84
130
``` java
85
-
131
+ class Solution {
132
+ private int m, n;
133
+ private long st = 0 ;
134
+ private List<List<Integer > > path = new ArrayList<> ();
135
+ private final int [] dirs = {- 1 , 0 , 1 , 0 , - 1 };
136
+
137
+ private int f (int i , int j ) {
138
+ return i * n + j;
139
+ }
140
+
141
+ private boolean dfs (int i , int j , int v , int [][] grid ) {
142
+ path. add(Arrays . asList(i, j));
143
+ if (path. size() == m * n) {
144
+ return true ;
145
+ }
146
+ st |= 1L << f(i, j);
147
+ if (grid[i][j] == v) {
148
+ v += 1 ;
149
+ }
150
+ for (int t = 0 ; t < 4 ; t++ ) {
151
+ int a = dirs[t], b = dirs[t + 1 ];
152
+ int x = i + a, y = j + b;
153
+ if (0 <= x && x < m && 0 <= y && y < n && (st & (1L << f(x, y))) == 0
154
+ && (grid[x][y] == 0 || grid[x][y] == v)) {
155
+ if (dfs(x, y, v, grid)) {
156
+ return true ;
157
+ }
158
+ }
159
+ }
160
+ path. remove(path. size() - 1 );
161
+ st ^ = 1L << f(i, j);
162
+ return false ;
163
+ }
164
+
165
+ public List<List<Integer > > findPath (int [][] grid , int k ) {
166
+ m = grid. length;
167
+ n = grid[0 ]. length;
168
+ for (int i = 0 ; i < m; i++ ) {
169
+ for (int j = 0 ; j < n; j++ ) {
170
+ if (grid[i][j] == 0 || grid[i][j] == 1 ) {
171
+ if (dfs(i, j, 1 , grid)) {
172
+ return path;
173
+ }
174
+ path. clear();
175
+ st = 0 ;
176
+ }
177
+ }
178
+ }
179
+ return List . of();
180
+ }
181
+ }
86
182
```
87
183
88
184
#### C++
89
185
90
186
``` cpp
91
-
187
+ class Solution {
188
+ int m, n;
189
+ unsigned long long st = 0;
190
+ vector<vector<int >> path;
191
+ int dirs[ 5] = {-1, 0, 1, 0, -1};
192
+
193
+ int f(int i, int j) {
194
+ return i * n + j;
195
+ }
196
+
197
+ bool dfs (int i, int j, int v, vector<vector<int >>& grid) {
198
+ path.push_back({i, j});
199
+ if (path.size() == static_cast<size_t>(m * n)) {
200
+ return true;
201
+ }
202
+ st |= 1ULL << f(i, j);
203
+ if (grid[ i] [ j ] == v) {
204
+ v += 1;
205
+ }
206
+ for (int t = 0; t < 4; ++t) {
207
+ int a = dirs[ t] , b = dirs[ t + 1] ;
208
+ int x = i + a, y = j + b;
209
+ if (0 <= x && x < m && 0 <= y && y < n && (st & (1ULL << f(x, y))) == 0
210
+ && (grid[ x] [ y ] == 0 || grid[ x] [ y ] == v)) {
211
+ if (dfs(x, y, v, grid)) {
212
+ return true;
213
+ }
214
+ }
215
+ }
216
+ path.pop_back();
217
+ st ^= 1ULL << f(i, j);
218
+ return false;
219
+ }
220
+
221
+ public:
222
+ vector<vector<int >> findPath(vector<vector<int >>& grid, int k) {
223
+ m = grid.size();
224
+ n = grid[ 0] .size();
225
+ for (int i = 0; i < m; ++i) {
226
+ for (int j = 0; j < n; ++j) {
227
+ if (grid[ i] [ j ] == 0 || grid[ i] [ j ] == 1) {
228
+ if (dfs(i, j, 1, grid)) {
229
+ return path;
230
+ }
231
+ path.clear();
232
+ st = 0;
233
+ }
234
+ }
235
+ }
236
+ return {};
237
+ }
238
+ };
92
239
```
93
240
94
241
#### Go
95
242
96
243
```go
244
+ func findPath(grid [][]int, k int) [][]int {
245
+ _ = k
246
+ m := len(grid)
247
+ n := len(grid[0])
248
+ var st uint64
249
+ path := [][]int{}
250
+ dirs := []int{-1, 0, 1, 0, -1}
251
+
252
+ f := func(i, j int) int { return i*n + j }
253
+
254
+ var dfs func(int, int, int) bool
255
+ dfs = func(i, j, v int) bool {
256
+ path = append(path, []int{i, j})
257
+ if len(path) == m*n {
258
+ return true
259
+ }
260
+ idx := f(i, j)
261
+ st |= 1 << idx
262
+ if grid[i][j] == v {
263
+ v++
264
+ }
265
+ for t := 0; t < 4; t++ {
266
+ a, b := dirs[t], dirs[t+1]
267
+ x, y := i+a, j+b
268
+ if 0 <= x && x < m && 0 <= y && y < n {
269
+ idx2 := f(x, y)
270
+ if (st>>idx2)&1 == 0 && (grid[x][y] == 0 || grid[x][y] == v) {
271
+ if dfs(x, y, v) {
272
+ return true
273
+ }
274
+ }
275
+ }
276
+ }
277
+ path = path[:len(path)-1]
278
+ st ^= 1 << idx
279
+ return false
280
+ }
281
+
282
+ for i := 0; i < m; i++ {
283
+ for j := 0; j < n; j++ {
284
+ if grid[i][j] == 0 || grid[i][j] == 1 {
285
+ if dfs(i, j, 1) {
286
+ return path
287
+ }
288
+ path = path[:0]
289
+ st = 0
290
+ }
291
+ }
292
+ }
293
+ return [][]int{}
294
+ }
295
+ ```
97
296
297
+ #### TypeScript
298
+
299
+ ``` ts
300
+ function findPath(grid : number [][], k : number ): number [][] {
301
+ const m = grid .length ;
302
+ const n = grid [0 ].length ;
303
+
304
+ const dirs = [- 1 , 0 , 1 , 0 , - 1 ];
305
+ const path: number [][] = [];
306
+ let st = 0 ;
307
+
308
+ function f(i : number , j : number ): number {
309
+ return i * n + j ;
310
+ }
311
+
312
+ function dfs(i : number , j : number , v : number ): boolean {
313
+ path .push ([i , j ]);
314
+ if (path .length === m * n ) {
315
+ return true ;
316
+ }
317
+
318
+ st |= 1 << f (i , j );
319
+ if (grid [i ][j ] === v ) {
320
+ v += 1 ;
321
+ }
322
+
323
+ for (let d = 0 ; d < 4 ; d ++ ) {
324
+ const x = i + dirs [d ];
325
+ const y = j + dirs [d + 1 ];
326
+ const pos = f (x , y );
327
+ if (
328
+ x >= 0 &&
329
+ x < m &&
330
+ y >= 0 &&
331
+ y < n &&
332
+ (st & (1 << pos )) === 0 &&
333
+ (grid [x ][y ] === 0 || grid [x ][y ] === v )
334
+ ) {
335
+ if (dfs (x , y , v )) {
336
+ return true ;
337
+ }
338
+ }
339
+ }
340
+
341
+ path .pop ();
342
+ st ^= 1 << f (i , j );
343
+ return false ;
344
+ }
345
+
346
+ for (let i = 0 ; i < m ; i ++ ) {
347
+ for (let j = 0 ; j < n ; j ++ ) {
348
+ if (grid [i ][j ] === 0 || grid [i ][j ] === 1 ) {
349
+ st = 0 ;
350
+ path .length = 0 ;
351
+ if (dfs (i , j , 1 )) {
352
+ return path ;
353
+ }
354
+ }
355
+ }
356
+ }
357
+
358
+ return [];
359
+ }
98
360
```
99
361
100
362
<!-- tabs:end -->
0 commit comments