small-lambda
Senior Member
Multi-source BFS + Max Priority Queue Dijkstra
Code:
use std::collections::*;
use std::cmp::*;
#[derive(Debug)]
struct Pair {
indices: (usize, usize),
safeness: i32
}
impl PartialEq for Pair {
fn eq(&self, other: &Pair) -> bool {
self.safeness.eq(&other.safeness)
}
}
impl Eq for Pair {}
impl PartialOrd<Pair> for Pair {
fn partial_cmp(&self, other: &Pair) -> Option<Ordering> {
self.safeness.partial_cmp(&other.safeness)
}
}
impl Ord for Pair {
fn cmp(&self, other: &Pair) -> Ordering {
self.safeness.cmp(&other.safeness)
}
}
impl Solution {
// 1. Use modified BFS to find min distance to any thief.
// 2. Use modified Dijkstra to find max safeness factor of paths to (n - 1, n- 1).
pub fn maximum_safeness_factor(mut grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
let mut queue = VecDeque::new();
for i in 0..n {
for j in 0..n {
if grid[i][j] == 1 {
queue.push_back((i, j));
}
}
}
while let Some((i, j)) = queue.pop_front() {
if i > 0 && grid[i - 1][j] == 0 {
grid[i - 1][j] = grid[i][j] + 1;
queue.push_back((i - 1, j));
}
if j < n - 1 && grid[i][j + 1] == 0 {
grid[i][j + 1] = grid[i][j] + 1;
queue.push_back((i, j + 1));
}
if i < n - 1 && grid[i + 1][j] == 0 {
grid[i + 1][j] = grid[i][j] + 1;
queue.push_back((i + 1, j));
}
if j > 0 && grid[i][j - 1] == 0 {
grid[i][j - 1] = grid[i][j] + 1;
queue.push_back((i, j - 1));
}
}
let mut max_heap = BinaryHeap::new();
let mut estimates = vec![vec![i32::MAX; n]; n];
estimates[0][0] = grid[0][0];
max_heap.push(
Pair {
indices: (0, 0),
safeness: estimates[0][0]
}
);
while let Some(
Pair {
indices: (i, j),
safeness: safeness
}
) = max_heap.pop() {
if safeness < estimates[i][j] {
continue;
}
if i == n - 1 && j == n - 1 {
return safeness - 1;
}
if i > 0 && estimates[i - 1][j] == i32::MAX {
estimates[i - 1][j] = estimates[i][j].min(grid[i - 1][j]);
max_heap.push(
Pair {
indices: (i - 1, j),
safeness: estimates[i - 1][j]
}
);
}
if j < n - 1 && estimates[i][j + 1] == i32::MAX {
estimates[i][j + 1] = estimates[i][j].min(grid[i][j + 1]);
max_heap.push(
Pair {
indices: (i, j + 1),
safeness: estimates[i][j + 1]
}
);
}
if i < n - 1 && estimates[i + 1][j] == i32::MAX {
estimates[i + 1][j] = estimates[i][j].min(grid[i + 1][j]);
max_heap.push(
Pair {
indices: (i + 1, j),
safeness: estimates[i + 1][j]
}
);
}
if j > 0 && estimates[i][j - 1] == i32::MAX {
estimates[i][j - 1] = estimates[i][j].min(grid[i][j- 1]);
max_heap.push(
Pair {
indices: (i, j - 1),
safeness: estimates[i][j - 1]
}
);
}
}
-1
}
}
Code:
use std::collections::*;
use std::cmp::*;
#[derive(Debug)]
struct Pair {
indices: (usize, usize),
safeness: i32
}
impl PartialEq for Pair {
fn eq(&self, other: &Pair) -> bool {
self.safeness.eq(&other.safeness)
}
}
impl Eq for Pair {}
impl PartialOrd<Pair> for Pair {
fn partial_cmp(&self, other: &Pair) -> Option<Ordering> {
self.safeness.partial_cmp(&other.safeness)
}
}
impl Ord for Pair {
fn cmp(&self, other: &Pair) -> Ordering {
self.safeness.cmp(&other.safeness)
}
}
impl Solution {
// 1. Use modified BFS to find min distance to any thief.
// 2. Use modified Dijkstra to find max safeness factor of paths to (n - 1, n- 1).
pub fn maximum_safeness_factor(mut grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
let mut queue = VecDeque::new();
let in_bounds = |r, c| { r < n && c < n};
for i in 0..n {
for j in 0..n {
if grid[i][j] == 1 {
queue.push_back((i, j));
}
}
}
while let Some((i, j)) = queue.pop_front() {
let neighbours = [(i - 1, j), (i, j + 1), (i + 1, j), (i, j - 1)];
for (r, c) in neighbours {
if in_bounds(r, c) && grid[r][c] == 0 {
grid[r][c] = grid[i][j] + 1;
queue.push_back((r, c));
}
}
}
let mut max_heap = BinaryHeap::new();
let mut estimates = vec![vec![i32::MAX; n]; n];
estimates[0][0] = grid[0][0];
max_heap.push(
Pair {
indices: (0, 0),
safeness: estimates[0][0]
}
);
while let Some(
Pair {
indices: (i, j),
safeness: safeness
}
) = max_heap.pop() {
if i == n - 1 && j == n - 1 {
return safeness - 1;
}
let neighbours = [(i - 1, j), (i, j + 1), (i + 1, j), (i, j - 1)];
for (r, c) in neighbours {
if in_bounds(r, c) && estimates[r][c] == i32::MAX {
estimates[r][c] = estimates[i][j].min(grid[r][c]);
max_heap.push(
Pair {
indices: (r, c),
safeness: estimates[r][c]
}
);
}
}
}
-1
}
}
Last edited: