Skip to content

Commit 7a6fb49

Browse files
committed
feat: add solutions to lcci problem: No.08.02
No.08.02.Robot in a Grid
1 parent 00dbcac commit 7a6fb49

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,62 @@
5050

5151
```
5252

53+
### **TypeScript**
54+
55+
```ts
56+
function pathWithObstacles(obstacleGrid: number[][]): number[][] {
57+
const m = obstacleGrid.length;
58+
const n = obstacleGrid[0].length;
59+
const res = [];
60+
const dfs = (i: number, j: number): boolean => {
61+
if (i === m || j === n || obstacleGrid[i][j] === 1) {
62+
return false;
63+
}
64+
res.push([i, j]);
65+
obstacleGrid[i][j] = 1;
66+
if ((i + 1 === m && j + 1 === n) || dfs(i + 1, j) || dfs(i, j + 1)) {
67+
return true;
68+
}
69+
res.pop();
70+
return false;
71+
};
72+
if (dfs(0, 0)) {
73+
return res;
74+
}
75+
return [];
76+
}
77+
```
78+
79+
### **Rust**
80+
81+
```rust
82+
impl Solution {
83+
fn dfs(grid: &mut Vec<Vec<i32>>, path: &mut Vec<Vec<i32>>, i: usize, j: usize) -> bool {
84+
if i == grid.len() || j == grid[0].len() || grid[i][j] == 1 {
85+
return false;
86+
}
87+
path.push(vec![i as i32, j as i32]);
88+
grid[i as usize][j as usize] = 1;
89+
if (i + 1 == grid.len() && j + 1 == grid[0].len())
90+
|| Self::dfs(grid, path, i + 1, j)
91+
|| Self::dfs(grid, path, i, j + 1)
92+
{
93+
return true;
94+
}
95+
path.pop();
96+
false
97+
}
98+
99+
pub fn path_with_obstacles(mut obstacle_grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
100+
let mut res = vec![];
101+
if Self::dfs(&mut obstacle_grid, &mut res, 0, 0) {
102+
return res;
103+
}
104+
vec![]
105+
}
106+
}
107+
```
108+
53109
### **...**
54110

55111
```

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,62 @@
5252

5353
```
5454

55+
### **TypeScript**
56+
57+
```ts
58+
function pathWithObstacles(obstacleGrid: number[][]): number[][] {
59+
const m = obstacleGrid.length;
60+
const n = obstacleGrid[0].length;
61+
const res = [];
62+
const dfs = (i: number, j: number): boolean => {
63+
if (i === m || j === n || obstacleGrid[i][j] === 1) {
64+
return false;
65+
}
66+
res.push([i, j]);
67+
obstacleGrid[i][j] = 1;
68+
if ((i + 1 === m && j + 1 === n) || dfs(i + 1, j) || dfs(i, j + 1)) {
69+
return true;
70+
}
71+
res.pop();
72+
return false;
73+
};
74+
if (dfs(0, 0)) {
75+
return res;
76+
}
77+
return [];
78+
}
79+
```
80+
81+
### **Rust**
82+
83+
```rust
84+
impl Solution {
85+
fn dfs(grid: &mut Vec<Vec<i32>>, path: &mut Vec<Vec<i32>>, i: usize, j: usize) -> bool {
86+
if i == grid.len() || j == grid[0].len() || grid[i][j] == 1 {
87+
return false;
88+
}
89+
path.push(vec![i as i32, j as i32]);
90+
grid[i as usize][j as usize] = 1;
91+
if (i + 1 == grid.len() && j + 1 == grid[0].len())
92+
|| Self::dfs(grid, path, i + 1, j)
93+
|| Self::dfs(grid, path, i, j + 1)
94+
{
95+
return true;
96+
}
97+
path.pop();
98+
false
99+
}
100+
101+
pub fn path_with_obstacles(mut obstacle_grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
102+
let mut res = vec![];
103+
if Self::dfs(&mut obstacle_grid, &mut res, 0, 0) {
104+
return res;
105+
}
106+
vec![]
107+
}
108+
}
109+
```
110+
55111
### **...**
56112

57113
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
impl Solution {
2+
fn dfs(grid: &mut Vec<Vec<i32>>, path: &mut Vec<Vec<i32>>, i: usize, j: usize) -> bool {
3+
if i == grid.len() || j == grid[0].len() || grid[i][j] == 1 {
4+
return false;
5+
}
6+
path.push(vec![i as i32, j as i32]);
7+
grid[i as usize][j as usize] = 1;
8+
if (i + 1 == grid.len() && j + 1 == grid[0].len())
9+
|| Self::dfs(grid, path, i + 1, j)
10+
|| Self::dfs(grid, path, i, j + 1)
11+
{
12+
return true;
13+
}
14+
path.pop();
15+
false
16+
}
17+
18+
pub fn path_with_obstacles(mut obstacle_grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
19+
let mut res = vec![];
20+
if Self::dfs(&mut obstacle_grid, &mut res, 0, 0) {
21+
return res;
22+
}
23+
vec![]
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function pathWithObstacles(obstacleGrid: number[][]): number[][] {
2+
const m = obstacleGrid.length;
3+
const n = obstacleGrid[0].length;
4+
const res = [];
5+
const dfs = (i: number, j: number): boolean => {
6+
if (i === m || j === n || obstacleGrid[i][j] === 1) {
7+
return false;
8+
}
9+
res.push([i, j]);
10+
obstacleGrid[i][j] = 1;
11+
if ((i + 1 === m && j + 1 === n) || dfs(i + 1, j) || dfs(i, j + 1)) {
12+
return true;
13+
}
14+
res.pop();
15+
return false;
16+
};
17+
if (dfs(0, 0)) {
18+
return res;
19+
}
20+
return [];
21+
}

0 commit comments

Comments
 (0)