934. Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.)

Now, we may change 0s to 1s so as to connect the two islands together to form 1 island.

Return the smallest number of 0s that must be flipped.  (It is guaranteed that the answer is at least 1.)

 

Example 1:

Input: A = [[0,1],[1,0]]
Output: 1

Example 2:

Input: A = [[0,1,0],[0,0,0],[0,0,1]]
Output: 2

Example 3:

Input: A = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
Output: 1

 

Constraints:

  • 2 <= A.length == A[0].length <= 100
  • A[i][j] == 0 or A[i][j] == 1

Rust Solution

struct Solution;
use std::collections::VecDeque;

impl Solution {
    fn shortest_bridge(mut a: Vec<Vec<i32>>) -> i32 {
        let n = a.len();
        let m = a[0].len();
        let mut queue: VecDeque<(usize, usize, i32)> = VecDeque::new();
        let mut found = false;
        for i in 0..n {
            if found {
                break;
            }
            for j in 0..m {
                if a[i][j] == 1 {
                    Self::dfs(i, j, &mut queue, &mut a, n, m);
                    found = true;
                    break;
                }
            }
        }
        while let Some((i, j, d)) = queue.pop_front() {
            match a[i][j] {
                0 | 2 => {
                    a[i][j] = 3;
                    if i > 0 && a[i - 1][j] < 2 {
                        queue.push_back((i - 1, j, d + 1));
                    }
                    if j > 0 && a[i][j - 1] < 2 {
                        queue.push_back((i, j - 1, d + 1));
                    }
                    if i + 1 < n && a[i + 1][j] < 2 {
                        queue.push_back((i + 1, j, d + 1));
                    }
                    if j + 1 < m && a[i][j + 1] < 2 {
                        queue.push_back((i, j + 1, d + 1));
                    }
                }
                1 => {
                    return d - 1;
                }
                _ => {}
            }
        }
        0
    }

    fn dfs(
        i: usize,
        j: usize,
        queue: &mut VecDeque<(usize, usize, i32)>,
        a: &mut Vec<Vec<i32>>,
        n: usize,
        m: usize,
    ) {
        if a[i][j] == 1 {
            a[i][j] = 2;
            queue.push_back((i, j, 0));
            if i > 0 {
                Self::dfs(i - 1, j, queue, a, n, m);
            }
            if j > 0 {
                Self::dfs(i, j - 1, queue, a, n, m);
            }
            if i + 1 < n {
                Self::dfs(i + 1, j, queue, a, n, m);
            }
            if j + 1 < m {
                Self::dfs(i, j + 1, queue, a, n, m);
            }
        }
    }
}

#[test]
fn test() {
    let a = vec_vec_i32![[0, 1], [1, 0]];
    let res = 1;
    assert_eq!(Solution::shortest_bridge(a), res);
    let a = vec_vec_i32![[0, 1, 0], [0, 0, 0], [0, 0, 1]];
    let res = 2;
    assert_eq!(Solution::shortest_bridge(a), res);
    let a = vec_vec_i32![
        [1, 1, 1, 1, 1],
        [1, 0, 0, 0, 1],
        [1, 0, 1, 0, 1],
        [1, 0, 0, 0, 1],
        [1, 1, 1, 1, 1]
    ];
    let res = 1;
    assert_eq!(Solution::shortest_bridge(a), res);
}

Having problems with this solution? Click here to submit an issue on github.