317. Shortest Distance from All Buildings
You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or 2, where:
- Each 0 marks an empty land which you can pass by freely.
- Each 1 marks a building which you cannot pass through.
- Each 2 marks an obstacle which you cannot pass through.
Example:
Input: [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]] 1 - 0 - 2 - 0 - 1 | | | | | 0 - 0 - 0 - 0 - 0 | | | | | 0 - 0 - 1 - 0 - 0 Output: 7 Explanation: Given three buildings at(0,0)
,(0,4)
,(2,2)
, and an obstacle at(0,2), t
he point(1,2)
is an ideal empty land to build a house, as the total travel distance of 3+3+1=7 is minimal. So return 7.
Note:
There will be at least one building. If it is not possible to build such house according to the above rules, return -1.
Rust Solution
struct Solution;
use std::collections::VecDeque;
impl Solution {
fn shortest_distance(grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
let m = grid[0].len();
let mut dist = vec![vec![0; m]; n];
let mut count = vec![vec![0; m]; n];
let mut building = 0;
for i in 0..n {
for j in 0..m {
if grid[i][j] == 1 {
building += 1;
}
}
}
for i in 0..n {
for j in 0..m {
if grid[i][j] == 1 {
Self::bfs(i, j, &mut dist, &mut count, &grid, m, n)
}
}
}
let mut res = std::usize::MAX;
for i in 0..n {
for j in 0..m {
if grid[i][j] == 0 && count[i][j] == building {
res = res.min(dist[i][j]);
}
}
}
if res == std::usize::MAX {
-1
} else {
res as i32
}
}
fn bfs(
i: usize,
j: usize,
dist: &mut Vec<Vec<usize>>,
count: &mut Vec<Vec<usize>>,
grid: &[Vec<i32>],
m: usize,
n: usize,
) {
let mut visited = vec![vec![false; m]; n];
let mut queue: VecDeque<(usize, usize, usize)> = VecDeque::new();
queue.push_back((i, j, 0));
while let Some((i, j, d)) = queue.pop_front() {
if i > 0 && grid[i - 1][j] == 0 && !visited[i - 1][j] {
visited[i - 1][j] = true;
dist[i - 1][j] += d + 1;
count[i - 1][j] += 1;
queue.push_back((i - 1, j, d + 1));
}
if j > 0 && grid[i][j - 1] == 0 && !visited[i][j - 1] {
visited[i][j - 1] = true;
dist[i][j - 1] += d + 1;
count[i][j - 1] += 1;
queue.push_back((i, j - 1, d + 1));
}
if i + 1 < n && grid[i + 1][j] == 0 && !visited[i + 1][j] {
visited[i + 1][j] = true;
dist[i + 1][j] += d + 1;
count[i + 1][j] += 1;
queue.push_back((i + 1, j, d + 1));
}
if j + 1 < m && grid[i][j + 1] == 0 && !visited[i][j + 1] {
visited[i][j + 1] = true;
dist[i][j + 1] += d + 1;
count[i][j + 1] += 1;
queue.push_back((i, j + 1, d + 1));
}
}
}
}
#[test]
fn test() {
let grid = vec_vec_i32![[1, 0, 2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0]];
let res = 7;
assert_eq!(Solution::shortest_distance(grid), res);
let grid = vec_vec_i32![
[1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 1],
[0, 1, 1, 0, 0, 1],
[1, 0, 0, 1, 0, 1],
[1, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[0, 1, 1, 1, 1, 0]
];
let res = 88;
assert_eq!(Solution::shortest_distance(grid), res);
}
Having problems with this solution? Click here to submit an issue on github.