Skip to content

Commit a9951a0

Browse files
committed
feat: add solutions to lcci problem: No.08.02
No.08.02.Robot in a Grid
1 parent 749f7c6 commit a9951a0

File tree

6 files changed

+273
-5
lines changed

6 files changed

+273
-5
lines changed

lcci/08.02.Robot in a Grid/README.md

+98-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626

2727
<!-- 这里可写通用的实现逻辑 -->
2828

29-
DFS 思路
29+
**方法一:DFS**
3030

31-
**剪枝:**
32-
防止多次进入同一个位置,走过的位置要将其置为 1
31+
我们可以使用深度优先搜索来解决本题。我们从左上角开始,向右或向下移动,直到到达右下角。如果在某一步,我们发现当前位置是障碍物,或者当前位置已经在路径中,那么我们就返回,否则我们将当前位置加入路径中,并且标记当前位置为已经访问过,然后继续向右或向下移动。
32+
33+
如果最终能够到达右下角,那么我们就找到了一条可行的路径,否则说明不存在可行的路径。
34+
35+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是网格的行数和列数。
3336

3437
<!-- tabs:start -->
3538

@@ -38,15 +41,106 @@ DFS 思路
3841
<!-- 这里可写当前语言的特殊实现逻辑 -->
3942

4043
```python
41-
44+
class Solution:
45+
def pathWithObstacles(self, obstacleGrid: List[List[int]]) -> List[List[int]]:
46+
def dfs(i, j):
47+
if i >= m or j >= n or obstacleGrid[i][j] == 1:
48+
return False
49+
ans.append([i, j])
50+
obstacleGrid[i][j] = 1
51+
if (i == m - 1 and j == n - 1) or dfs(i + 1, j) or dfs(i, j + 1):
52+
return True
53+
ans.pop()
54+
return False
55+
56+
m, n = len(obstacleGrid), len(obstacleGrid[0])
57+
ans = []
58+
return ans if dfs(0, 0) else []
4259
```
4360

4461
### **Java**
4562

4663
<!-- 这里可写当前语言的特殊实现逻辑 -->
4764

4865
```java
66+
class Solution {
67+
private List<List<Integer>> ans = new ArrayList<>();
68+
private int[][] g;
69+
private int m;
70+
private int n;
71+
72+
public List<List<Integer>> pathWithObstacles(int[][] obstacleGrid) {
73+
g = obstacleGrid;
74+
m = g.length;
75+
n = g[0].length;
76+
return dfs(0, 0) ? ans : Collections.emptyList();
77+
}
78+
79+
private boolean dfs(int i, int j) {
80+
if (i >= m || j >= n || g[i][j] == 1) {
81+
return false;
82+
}
83+
ans.add(List.of(i, j));
84+
g[i][j] = 1;
85+
if ((i == m - 1 && j == n - 1) || dfs(i + 1, j) || dfs(i, j + 1)) {
86+
return true;
87+
}
88+
ans.remove(ans.size() - 1);
89+
return false;
90+
}
91+
}
92+
```
4993

94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
vector<vector<int>> pathWithObstacles(vector<vector<int>>& obstacleGrid) {
100+
int m = obstacleGrid.size();
101+
int n = obstacleGrid[0].size();
102+
vector<vector<int>> ans;
103+
function<bool(int, int)> dfs = [&](int i, int j) -> bool {
104+
if (i >= m || j >= n || obstacleGrid[i][j] == 1) {
105+
return false;
106+
}
107+
ans.push_back({i, j});
108+
obstacleGrid[i][j] = 1;
109+
if ((i == m - 1 && j == n - 1) || dfs(i + 1, j) || dfs(i, j + 1)) {
110+
return true;
111+
}
112+
ans.pop_back();
113+
return false;
114+
};
115+
return dfs(0, 0) ? ans : vector<vector<int>>();
116+
}
117+
};
118+
```
119+
120+
### **Go**
121+
122+
```go
123+
func pathWithObstacles(obstacleGrid [][]int) [][]int {
124+
m, n := len(obstacleGrid), len(obstacleGrid[0])
125+
ans := [][]int{}
126+
var dfs func(i, j int) bool
127+
dfs = func(i, j int) bool {
128+
if i >= m || j >= n || obstacleGrid[i][j] == 1 {
129+
return false
130+
}
131+
ans = append(ans, []int{i, j})
132+
obstacleGrid[i][j] = 1
133+
if (i == m-1 && j == n-1) || dfs(i+1, j) || dfs(i, j+1) {
134+
return true
135+
}
136+
ans = ans[:len(ans)-1]
137+
return false
138+
}
139+
if dfs(0, 0) {
140+
return ans
141+
}
142+
return [][]int{}
143+
}
50144
```
51145

52146
### **TypeScript**

lcci/08.02.Robot in a Grid/README_EN.md

+92-1
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,104 @@
3737
### **Python3**
3838

3939
```python
40-
40+
class Solution:
41+
def pathWithObstacles(self, obstacleGrid: List[List[int]]) -> List[List[int]]:
42+
def dfs(i, j):
43+
if i >= m or j >= n or obstacleGrid[i][j] == 1:
44+
return False
45+
ans.append([i, j])
46+
obstacleGrid[i][j] = 1
47+
if (i == m - 1 and j == n - 1) or dfs(i + 1, j) or dfs(i, j + 1):
48+
return True
49+
ans.pop()
50+
return False
51+
52+
m, n = len(obstacleGrid), len(obstacleGrid[0])
53+
ans = []
54+
return ans if dfs(0, 0) else []
4155
```
4256

4357
### **Java**
4458

4559
```java
60+
class Solution {
61+
private List<List<Integer>> ans = new ArrayList<>();
62+
private int[][] g;
63+
private int m;
64+
private int n;
65+
66+
public List<List<Integer>> pathWithObstacles(int[][] obstacleGrid) {
67+
g = obstacleGrid;
68+
m = g.length;
69+
n = g[0].length;
70+
return dfs(0, 0) ? ans : Collections.emptyList();
71+
}
72+
73+
private boolean dfs(int i, int j) {
74+
if (i >= m || j >= n || g[i][j] == 1) {
75+
return false;
76+
}
77+
ans.add(List.of(i, j));
78+
g[i][j] = 1;
79+
if ((i == m - 1 && j == n - 1) || dfs(i + 1, j) || dfs(i, j + 1)) {
80+
return true;
81+
}
82+
ans.remove(ans.size() - 1);
83+
return false;
84+
}
85+
}
86+
```
87+
88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
vector<vector<int>> pathWithObstacles(vector<vector<int>>& obstacleGrid) {
94+
int m = obstacleGrid.size();
95+
int n = obstacleGrid[0].size();
96+
vector<vector<int>> ans;
97+
function<bool(int, int)> dfs = [&](int i, int j) -> bool {
98+
if (i >= m || j >= n || obstacleGrid[i][j] == 1) {
99+
return false;
100+
}
101+
ans.push_back({i, j});
102+
obstacleGrid[i][j] = 1;
103+
if ((i == m - 1 && j == n - 1) || dfs(i + 1, j) || dfs(i, j + 1)) {
104+
return true;
105+
}
106+
ans.pop_back();
107+
return false;
108+
};
109+
return dfs(0, 0) ? ans : vector<vector<int>>();
110+
}
111+
};
112+
```
46113
114+
### **Go**
115+
116+
```go
117+
func pathWithObstacles(obstacleGrid [][]int) [][]int {
118+
m, n := len(obstacleGrid), len(obstacleGrid[0])
119+
ans := [][]int{}
120+
var dfs func(i, j int) bool
121+
dfs = func(i, j int) bool {
122+
if i >= m || j >= n || obstacleGrid[i][j] == 1 {
123+
return false
124+
}
125+
ans = append(ans, []int{i, j})
126+
obstacleGrid[i][j] = 1
127+
if (i == m-1 && j == n-1) || dfs(i+1, j) || dfs(i, j+1) {
128+
return true
129+
}
130+
ans = ans[:len(ans)-1]
131+
return false
132+
}
133+
if dfs(0, 0) {
134+
return ans
135+
}
136+
return [][]int{}
137+
}
47138
```
48139

49140
### **TypeScript**
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> pathWithObstacles(vector<vector<int>>& obstacleGrid) {
4+
int m = obstacleGrid.size();
5+
int n = obstacleGrid[0].size();
6+
vector<vector<int>> ans;
7+
function<bool(int, int)> dfs = [&](int i, int j) -> bool {
8+
if (i >= m || j >= n || obstacleGrid[i][j] == 1) {
9+
return false;
10+
}
11+
ans.push_back({i, j});
12+
obstacleGrid[i][j] = 1;
13+
if ((i == m - 1 && j == n - 1) || dfs(i + 1, j) || dfs(i, j + 1)) {
14+
return true;
15+
}
16+
ans.pop_back();
17+
return false;
18+
};
19+
return dfs(0, 0) ? ans : vector<vector<int>>();
20+
}
21+
};
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func pathWithObstacles(obstacleGrid [][]int) [][]int {
2+
m, n := len(obstacleGrid), len(obstacleGrid[0])
3+
ans := [][]int{}
4+
var dfs func(i, j int) bool
5+
dfs = func(i, j int) bool {
6+
if i >= m || j >= n || obstacleGrid[i][j] == 1 {
7+
return false
8+
}
9+
ans = append(ans, []int{i, j})
10+
obstacleGrid[i][j] = 1
11+
if (i == m-1 && j == n-1) || dfs(i+1, j) || dfs(i, j+1) {
12+
return true
13+
}
14+
ans = ans[:len(ans)-1]
15+
return false
16+
}
17+
if dfs(0, 0) {
18+
return ans
19+
}
20+
return [][]int{}
21+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
private List<List<Integer>> ans = new ArrayList<>();
3+
private int[][] g;
4+
private int m;
5+
private int n;
6+
7+
public List<List<Integer>> pathWithObstacles(int[][] obstacleGrid) {
8+
g = obstacleGrid;
9+
m = g.length;
10+
n = g[0].length;
11+
return dfs(0, 0) ? ans : Collections.emptyList();
12+
}
13+
14+
private boolean dfs(int i, int j) {
15+
if (i >= m || j >= n || g[i][j] == 1) {
16+
return false;
17+
}
18+
ans.add(List.of(i, j));
19+
g[i][j] = 1;
20+
if ((i == m - 1 && j == n - 1) || dfs(i + 1, j) || dfs(i, j + 1)) {
21+
return true;
22+
}
23+
ans.remove(ans.size() - 1);
24+
return false;
25+
}
26+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def pathWithObstacles(self, obstacleGrid: List[List[int]]) -> List[List[int]]:
3+
def dfs(i, j):
4+
if i >= m or j >= n or obstacleGrid[i][j] == 1:
5+
return False
6+
ans.append([i, j])
7+
obstacleGrid[i][j] = 1
8+
if (i == m - 1 and j == n - 1) or dfs(i + 1, j) or dfs(i, j + 1):
9+
return True
10+
ans.pop()
11+
return False
12+
13+
m, n = len(obstacleGrid), len(obstacleGrid[0])
14+
ans = []
15+
return ans if dfs(0, 0) else []

0 commit comments

Comments
 (0)