<p>You are given an <code>n x n</code> binary matrix <code>grid</code> where <code>1</code> represents land and <code>0</code> represents water.</p>

<p>An <strong>island</strong> is a 4-directionally connected group of <code>1</code>&#39;s not connected to any other <code>1</code>&#39;s. There are <strong>exactly two islands</strong> in <code>grid</code>.</p>

<p>You may change <code>0</code>&#39;s to <code>1</code>&#39;s to connect the two islands to form <strong>one island</strong>.</p>

<p>Return <em>the smallest number of </em><code>0</code><em>&#39;s you must flip to connect the two islands</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [[0,1],[1,0]]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[0,1,0],[0,0,0],[0,0,1]]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[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]]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length == grid[i].length</code></li>
	<li><code>2 &lt;= n &lt;= 100</code></li>
	<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
	<li>There are exactly two islands in <code>grid</code>.</li>
</ul>

// The problem is finding the shortest path from one island to another island
// First do dfs and change one island to another letter
// Second do dfs and change the second island to another letter
// Pick the island with less nodes, and perform BFS from each nodes till you reach the other island
// Pick the shortest distance
// The above can be simplified using multi source BFS
// https://leetcode.com/problems/shortest-bridge/description/comments/1734246

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;

class Solution {
    vvi dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public:
    bool isWithinBoundary(int i, int j, int n) {
        if (i<0 || j<0 || i>=n || j>=n) return false;
        return true;
    }

    void dfs(vvi &grid, int i, int j, vvi &visited, queue<vi> &q) {
        int n = grid.size();

        if (!isWithinBoundary(i, j, n)) return;
        if (grid[i][j] == 0 || visited[i][j]) return;

        visited[i][j] = true;
        q.push({i, j, 0}); // {x, y, dist}

        // neighbor dfs traversal
        for(auto &dir: dirs) {
            int nx = i + dir[0];
            int ny = j + dir[1];
            if (isWithinBoundary(nx, ny, n)
                && grid[nx][ny] == 1
                && !visited[nx][ny]
            ) {
                dfs(grid, nx, ny, visited, q);
            }
        }
    }

    int shortestBridge(vector<vector<int>>& grid) {
        int n = grid.size();
        vvi visited(n, vi(n, false));
        queue<vi> q; // q for multi source bfs traversal {x, y, dist}

        // dfs
        bool foundOneIsland = false;
        for(int i=0; i<n; i++) {
            if (foundOneIsland)
                break;
            for(int j=0; j<n; j++) {
                if (grid[i][j] == 1) {
                    // we have found the first island, do dfs and add nodes for multi source bfs traversal
                    dfs(grid, i, j, visited, q);
                    // now we can break out of the loop since we have added nodes of one island
                    foundOneIsland = true;
                    break;
                }
            }
        }

        // Start multi-source BFS traversal from the added nodes
        while(!q.empty()) {
            auto front = q.front(); q.pop();
            int x = front[0], y = front[1];
            int d = front[2];

            if (!isWithinBoundary(x, y, n)) continue;
            if (visited[x][y] && grid[x][y] == 0) continue; // grid 0 becuz 1st island nodes shouldn't be continued

            if (!visited[x][y] && grid[x][y] == 1) {
                // We have reached the second island
                // (as it is not already visited, but grid has land)
                return d-1; // -1 because we need to ret the no. of 0 cells in between
            }

            visited[x][y] = true;

            // neighbor bfs traversal
            for(auto &dir: dirs) {
                int nx = x + dir[0];
                int ny = y + dir[1];
                
                if (!isWithinBoundary(nx, ny, n)) continue;
                if (visited[nx][ny]) continue;

                q.push({nx, ny, 1 + d});
            }

        }

        return -1;
    }
};
34735