use std::collections::{HashSet, VecDeque};
impl Solution { fn search(blocked: &Vec<Vec<i32>>, source: &Vec<i32>, target: &Vec<i32>) ->bool { const M : i32 = 1000000; let (mut block, mut visited) = (HashSet::new(), HashSet::new()); let (dx, dy) : ([i32; 4], [i32; 4]) = ([0, 0, 1, -1], [1, -1, 0, 0]); for i in blocked { block.insert((i[0], i[1])); } let mut q = VecDeque::new(); q.push_back((source[0], source[1])); visited.insert((source[0], source[1]));
while !q.is_empty() { if visited.len() > block.len() * block.len() / 2 {
return true; } let cur = q.front().unwrap().clone(); q.pop_front(); let (x, y) = (cur.0, cur.1); if x == target[0] && y == target[1] { return true; } for i in 0..4 { let (xx, yy) = (x + dx[i], y + dy[i]); if xx < 0 || yy < 0 || xx >= M || yy >= M || visited.contains(&(xx, yy)) || block.contains(&(xx, yy)) { continue; } q.push_back((xx, yy)); visited.insert((xx, yy)); } }
return false; }
pub fn is_escape_possible(blocked: Vec<Vec<i32>>, source: Vec<i32>, target: Vec<i32>) -> bool { return Self::search(&blocked, &source, &target) && Self::search(&blocked, &target, &source); } }
|