Skip to content

Commit d1cda2e

Browse files
committed
feat: corrent depth
1 parent 83e8311 commit d1cda2e

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

open_the_lock/src/lib.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,30 @@ impl Solution {
2727
}
2828

2929
pub fn open_lock(deadends: Vec<String>, target: String) -> i32 {
30-
1
30+
let mut marked = vec![false; 10000];
31+
let mut queue = vec![];
32+
let mut depth = 0;
33+
queue.push("0000".to_string());
34+
loop {
35+
let mut new_queue = vec![];
36+
for current in queue {
37+
if current == target {
38+
return depth;
39+
}
40+
for neighbor in Self::neighbors(&current) {
41+
let index: usize = neighbor.parse().unwrap();
42+
if !marked[index] {
43+
marked[index] = true;
44+
new_queue.push(neighbor);
45+
}
46+
}
47+
}
48+
if new_queue.is_empty() {
49+
return -1;
50+
}
51+
queue = new_queue;
52+
depth += 1;
53+
}
3154
}
3255
}
3356

0 commit comments

Comments
 (0)