Skip to content

Commit 5d2d454

Browse files
authored
feat: add solutions to lc problem: No.0980 (#1384)
No.0980.Unique Paths III
1 parent fb00253 commit 5d2d454

File tree

4 files changed

+178
-25
lines changed

4 files changed

+178
-25
lines changed

solution/0900-0999/0980.Unique Paths III/README.md

+51-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373
最后,我们返回从起点出发的路径数即可,即 $dfs(x, y, 1)$。
7474

75-
时间复杂度 $O(4^{m \times n})$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为网格的行数和列数。
75+
时间复杂度 $O(3^{m \times n})$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为网格的行数和列数。
7676

7777
<!-- tabs:start -->
7878

@@ -83,7 +83,7 @@
8383
```python
8484
class Solution:
8585
def uniquePathsIII(self, grid: List[List[int]]) -> int:
86-
def dfs(i, j, k):
86+
def dfs(i: int, j: int, k: int) -> int:
8787
if grid[i][j] == 2:
8888
return int(k == cnt + 1)
8989
ans = 0
@@ -98,7 +98,7 @@ class Solution:
9898
m, n = len(grid), len(grid[0])
9999
start = next((i, j) for i in range(m) for j in range(n) if grid[i][j] == 1)
100100
dirs = (-1, 0, 1, 0, -1)
101-
cnt = sum(grid[i][j] == 0 for i in range(m) for j in range(n))
101+
cnt = sum(row.count(0) for row in grid)
102102
vis = {start}
103103
return dfs(*start, 0)
104104
```
@@ -241,6 +241,54 @@ func uniquePathsIII(grid [][]int) int {
241241
}
242242
```
243243

244+
### **TypeScript**
245+
246+
```ts
247+
function uniquePathsIII(grid: number[][]): number {
248+
const m = grid.length;
249+
const n = grid[0].length;
250+
let [x, y] = [0, 0];
251+
let cnt = 0;
252+
for (let i = 0; i < m; ++i) {
253+
for (let j = 0; j < n; ++j) {
254+
if (grid[i][j] === 0) {
255+
++cnt;
256+
} else if (grid[i][j] == 1) {
257+
[x, y] = [i, j];
258+
}
259+
}
260+
}
261+
const vis: boolean[][] = Array(m)
262+
.fill(0)
263+
.map(() => Array(n).fill(false));
264+
vis[x][y] = true;
265+
const dirs = [-1, 0, 1, 0, -1];
266+
const dfs = (i: number, j: number, k: number): number => {
267+
if (grid[i][j] === 2) {
268+
return k === cnt + 1 ? 1 : 0;
269+
}
270+
let ans = 0;
271+
for (let d = 0; d < 4; ++d) {
272+
const [x, y] = [i + dirs[d], j + dirs[d + 1]];
273+
if (
274+
x >= 0 &&
275+
x < m &&
276+
y >= 0 &&
277+
y < n &&
278+
!vis[x][y] &&
279+
grid[x][y] !== -1
280+
) {
281+
vis[x][y] = true;
282+
ans += dfs(x, y, k + 1);
283+
vis[x][y] = false;
284+
}
285+
}
286+
return ans;
287+
};
288+
return dfs(x, y, 0);
289+
}
290+
```
291+
244292
### **...**
245293

246294
```

solution/0900-0999/0980.Unique Paths III/README_EN.md

+64-2
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,28 @@ Note that the starting and ending square can be anywhere in the grid.
6161

6262
## Solutions
6363

64+
**Solution 1: Backtracking**
65+
66+
We can first traverse the entire grid, find the starting point $(x, y)$, and count the number of blank spaces $cnt$.
67+
68+
Next, we can start searching from the starting point to get all the path numbers. We design a function $dfs(i, j, k)$ to indicate that the path number is $k$ and the starting point is $(i, j)$.
69+
70+
In the function, we first determine whether the current cell is the end point. If it is, we determine whether $k$ is equal to $cnt + 1$. If it is, the current path is a valid path, and $1$ is returned, otherwise $0$ is returned.
71+
72+
If the current cell is not the end point, we enumerate the four adjacent cells of the current cell. If the adjacent cell has not been visited, we mark the adjacent cell as visited, and then continue to search the path number from the adjacent cell. After the search is completed, we mark the adjacent cell as unvisited. After the search is completed, we return the sum of the path numbers of all adjacent cells.
73+
74+
Finally, we return the path number from the starting point, that is, $dfs(x, y, 1)$.
75+
76+
The time complexity is $O(3^{m \times n})$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns of the grid, respectively.
77+
6478
<!-- tabs:start -->
6579

6680
### **Python3**
6781

6882
```python
6983
class Solution:
7084
def uniquePathsIII(self, grid: List[List[int]]) -> int:
71-
def dfs(i, j, k):
85+
def dfs(i: int, j: int, k: int) -> int:
7286
if grid[i][j] == 2:
7387
return int(k == cnt + 1)
7488
ans = 0
@@ -83,7 +97,7 @@ class Solution:
8397
m, n = len(grid), len(grid[0])
8498
start = next((i, j) for i in range(m) for j in range(n) if grid[i][j] == 1)
8599
dirs = (-1, 0, 1, 0, -1)
86-
cnt = sum(grid[i][j] == 0 for i in range(m) for j in range(n))
100+
cnt = sum(row.count(0) for row in grid)
87101
vis = {start}
88102
return dfs(*start, 0)
89103
```
@@ -224,6 +238,54 @@ func uniquePathsIII(grid [][]int) int {
224238
}
225239
```
226240

241+
### **TypeScript**
242+
243+
```ts
244+
function uniquePathsIII(grid: number[][]): number {
245+
const m = grid.length;
246+
const n = grid[0].length;
247+
let [x, y] = [0, 0];
248+
let cnt = 0;
249+
for (let i = 0; i < m; ++i) {
250+
for (let j = 0; j < n; ++j) {
251+
if (grid[i][j] === 0) {
252+
++cnt;
253+
} else if (grid[i][j] == 1) {
254+
[x, y] = [i, j];
255+
}
256+
}
257+
}
258+
const vis: boolean[][] = Array(m)
259+
.fill(0)
260+
.map(() => Array(n).fill(false));
261+
vis[x][y] = true;
262+
const dirs = [-1, 0, 1, 0, -1];
263+
const dfs = (i: number, j: number, k: number): number => {
264+
if (grid[i][j] === 2) {
265+
return k === cnt + 1 ? 1 : 0;
266+
}
267+
let ans = 0;
268+
for (let d = 0; d < 4; ++d) {
269+
const [x, y] = [i + dirs[d], j + dirs[d + 1]];
270+
if (
271+
x >= 0 &&
272+
x < m &&
273+
y >= 0 &&
274+
y < n &&
275+
!vis[x][y] &&
276+
grid[x][y] !== -1
277+
) {
278+
vis[x][y] = true;
279+
ans += dfs(x, y, k + 1);
280+
vis[x][y] = false;
281+
}
282+
}
283+
return ans;
284+
};
285+
return dfs(x, y, 0);
286+
}
287+
```
288+
227289
### **...**
228290

229291
```
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
class Solution:
2-
def uniquePathsIII(self, grid: List[List[int]]) -> int:
3-
def dfs(i, j, k):
4-
if grid[i][j] == 2:
5-
return int(k == cnt + 1)
6-
ans = 0
7-
for a, b in pairwise(dirs):
8-
x, y = i + a, j + b
9-
if 0 <= x < m and 0 <= y < n and (x, y) not in vis and grid[x][y] != -1:
10-
vis.add((x, y))
11-
ans += dfs(x, y, k + 1)
12-
vis.remove((x, y))
13-
return ans
14-
15-
m, n = len(grid), len(grid[0])
16-
start = next((i, j) for i in range(m) for j in range(n) if grid[i][j] == 1)
17-
dirs = (-1, 0, 1, 0, -1)
18-
cnt = sum(grid[i][j] == 0 for i in range(m) for j in range(n))
19-
vis = {start}
20-
return dfs(*start, 0)
1+
class Solution:
2+
def uniquePathsIII(self, grid: List[List[int]]) -> int:
3+
def dfs(i: int, j: int, k: int) -> int:
4+
if grid[i][j] == 2:
5+
return int(k == cnt + 1)
6+
ans = 0
7+
for a, b in pairwise(dirs):
8+
x, y = i + a, j + b
9+
if 0 <= x < m and 0 <= y < n and (x, y) not in vis and grid[x][y] != -1:
10+
vis.add((x, y))
11+
ans += dfs(x, y, k + 1)
12+
vis.remove((x, y))
13+
return ans
14+
15+
m, n = len(grid), len(grid[0])
16+
start = next((i, j) for i in range(m) for j in range(n) if grid[i][j] == 1)
17+
dirs = (-1, 0, 1, 0, -1)
18+
cnt = sum(row.count(0) for row in grid)
19+
vis = {start}
20+
return dfs(*start, 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function uniquePathsIII(grid: number[][]): number {
2+
const m = grid.length;
3+
const n = grid[0].length;
4+
let [x, y] = [0, 0];
5+
let cnt = 0;
6+
for (let i = 0; i < m; ++i) {
7+
for (let j = 0; j < n; ++j) {
8+
if (grid[i][j] === 0) {
9+
++cnt;
10+
} else if (grid[i][j] == 1) {
11+
[x, y] = [i, j];
12+
}
13+
}
14+
}
15+
const vis: boolean[][] = Array(m)
16+
.fill(0)
17+
.map(() => Array(n).fill(false));
18+
vis[x][y] = true;
19+
const dirs = [-1, 0, 1, 0, -1];
20+
const dfs = (i: number, j: number, k: number): number => {
21+
if (grid[i][j] === 2) {
22+
return k === cnt + 1 ? 1 : 0;
23+
}
24+
let ans = 0;
25+
for (let d = 0; d < 4; ++d) {
26+
const [x, y] = [i + dirs[d], j + dirs[d + 1]];
27+
if (
28+
x >= 0 &&
29+
x < m &&
30+
y >= 0 &&
31+
y < n &&
32+
!vis[x][y] &&
33+
grid[x][y] !== -1
34+
) {
35+
vis[x][y] = true;
36+
ans += dfs(x, y, k + 1);
37+
vis[x][y] = false;
38+
}
39+
}
40+
return ans;
41+
};
42+
return dfs(x, y, 0);
43+
}

0 commit comments

Comments
 (0)