Skip to content

Commit 324d148

Browse files
committed
feat: add rust solution to lc problem: No.1036
No.1036.Escape a Large Maze
1 parent cb431c5 commit 324d148

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

solution/1000-1099/1036.Escape a Large Maze/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,57 @@ func isEscapePossible(blocked [][]int, source []int, target []int) bool {
186186
}
187187
```
188188

189+
### **Rust**
190+
191+
```rs
192+
use std::collections::{HashSet, VecDeque};
193+
194+
const BOUNDARY: i32 = 1_000_000;
195+
const MAX: usize = 20000;
196+
197+
impl Solution {
198+
pub fn is_escape_possible(blocked: Vec<Vec<i32>>, source: Vec<i32>, target: Vec<i32>) -> bool {
199+
let mut block = HashSet::with_capacity(blocked.len());
200+
for b in blocked.iter() {
201+
block.insert((b[0], b[1]));
202+
}
203+
bfs(&block, &source, &target) && bfs(&block, &target, &source)
204+
}
205+
}
206+
207+
fn bfs(block: &HashSet<(i32, i32)>, source: &Vec<i32>, target: &Vec<i32>) -> bool {
208+
let dir = vec![(-1, 0), (1, 0), (0, -1), (0, 1)];
209+
210+
let mut queue = VecDeque::new();
211+
let mut vis = HashSet::new();
212+
queue.push_back((source[0], source[1]));
213+
vis.insert((source[0], source[1]));
214+
215+
while !queue.is_empty() && vis.len() < MAX {
216+
let (x, y) = queue.pop_front().unwrap();
217+
if x == target[0] && y == target[1] {
218+
return true;
219+
}
220+
for (dx, dy) in dir.iter() {
221+
let (nx, ny) = (x + dx, y + dy);
222+
if nx < 0
223+
|| nx >= BOUNDARY
224+
|| ny < 0
225+
|| ny >= BOUNDARY
226+
|| vis.contains(&(nx, ny))
227+
|| block.contains(&(nx, ny))
228+
{
229+
continue;
230+
}
231+
queue.push_back((nx, ny));
232+
vis.insert((nx, ny));
233+
}
234+
}
235+
236+
vis.len() >= MAX
237+
}
238+
```
239+
189240
### **...**
190241

191242
```

solution/1000-1099/1036.Escape a Large Maze/README_EN.md

+51
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,57 @@ func isEscapePossible(blocked [][]int, source []int, target []int) bool {
175175
}
176176
```
177177

178+
### **Rust**
179+
180+
```rs
181+
use std::collections::{HashSet, VecDeque};
182+
183+
const BOUNDARY: i32 = 1_000_000;
184+
const MAX: usize = 20000;
185+
186+
impl Solution {
187+
pub fn is_escape_possible(blocked: Vec<Vec<i32>>, source: Vec<i32>, target: Vec<i32>) -> bool {
188+
let mut block = HashSet::with_capacity(blocked.len());
189+
for b in blocked.iter() {
190+
block.insert((b[0], b[1]));
191+
}
192+
bfs(&block, &source, &target) && bfs(&block, &target, &source)
193+
}
194+
}
195+
196+
fn bfs(block: &HashSet<(i32, i32)>, source: &Vec<i32>, target: &Vec<i32>) -> bool {
197+
let dir = vec![(-1, 0), (1, 0), (0, -1), (0, 1)];
198+
199+
let mut queue = VecDeque::new();
200+
let mut vis = HashSet::new();
201+
queue.push_back((source[0], source[1]));
202+
vis.insert((source[0], source[1]));
203+
204+
while !queue.is_empty() && vis.len() < MAX {
205+
let (x, y) = queue.pop_front().unwrap();
206+
if x == target[0] && y == target[1] {
207+
return true;
208+
}
209+
for (dx, dy) in dir.iter() {
210+
let (nx, ny) = (x + dx, y + dy);
211+
if nx < 0
212+
|| nx >= BOUNDARY
213+
|| ny < 0
214+
|| ny >= BOUNDARY
215+
|| vis.contains(&(nx, ny))
216+
|| block.contains(&(nx, ny))
217+
{
218+
continue;
219+
}
220+
queue.push_back((nx, ny));
221+
vis.insert((nx, ny));
222+
}
223+
}
224+
225+
vis.len() >= MAX
226+
}
227+
```
228+
178229
### **...**
179230

180231
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::collections::{HashSet, VecDeque};
2+
3+
const BOUNDARY: i32 = 1_000_000;
4+
const MAX: usize = 20000;
5+
6+
impl Solution {
7+
pub fn is_escape_possible(blocked: Vec<Vec<i32>>, source: Vec<i32>, target: Vec<i32>) -> bool {
8+
let mut block = HashSet::with_capacity(blocked.len());
9+
for b in blocked.iter() {
10+
block.insert((b[0], b[1]));
11+
}
12+
bfs(&block, &source, &target) && bfs(&block, &target, &source)
13+
}
14+
}
15+
16+
fn bfs(block: &HashSet<(i32, i32)>, source: &Vec<i32>, target: &Vec<i32>) -> bool {
17+
let dir = vec![(-1, 0), (1, 0), (0, -1), (0, 1)];
18+
19+
let mut queue = VecDeque::new();
20+
let mut vis = HashSet::new();
21+
queue.push_back((source[0], source[1]));
22+
vis.insert((source[0], source[1]));
23+
24+
while !queue.is_empty() && vis.len() < MAX {
25+
let (x, y) = queue.pop_front().unwrap();
26+
if x == target[0] && y == target[1] {
27+
return true;
28+
}
29+
for (dx, dy) in dir.iter() {
30+
let (nx, ny) = (x + dx, y + dy);
31+
if nx < 0
32+
|| nx >= BOUNDARY
33+
|| ny < 0
34+
|| ny >= BOUNDARY
35+
|| vis.contains(&(nx, ny))
36+
|| block.contains(&(nx, ny))
37+
{
38+
continue;
39+
}
40+
queue.push_back((nx, ny));
41+
vis.insert((nx, ny));
42+
}
43+
}
44+
45+
vis.len() >= MAX
46+
}

0 commit comments

Comments
 (0)