<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>

class Solution {
public:
    vector<vector<int>> directions = {
        {0,1},
        {1,0},
        {0,-1},
        {-1,0}
    };

    queue<pair<int,int>>q;

    int shortestBridge(vector<vector<int>>& grid) {
        int m = grid.size();
        if(!m)
            return 0;
        int n = grid[0].size();
        vector<vector<int>>visited(m+1, vector<int>(n+1, 0));

        int rowStart;
        int colStart;
        for(int i=0;i<m;i++)
        {
            int flag = 0;
            for(int j=n-1;j>=0;j--)
            {
                if(grid[i][j] == 1)
                {
                    rowStart = i;
                    colStart = j;
                    //traverse would mark the current island with '2'
                    grid[i][j] = 2;
                    traverse(grid, i, j, m, n, visited);
                    flag = 1;
                    break;
                }
            }
            if(flag)
                break;
        }

        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                cout<<grid[i][j]<<" ";
            }
            cout<<endl;
        }

    // Reset each element to a default value of 1
    for (auto& innerVector : visited) {
        for (int& element : innerVector) {
            element = 0;
        }
    }

        return findDistance(grid, rowStart, colStart, m, n);
    }

    void traverse(vector<vector<int>>& grid, int i, int j, int m, int n, vector<vector<int>>&visited)
    {
        q.push(make_pair(i,j));
        for(int k=0;k<4;k++)
        {
            int newRow = i + directions[k][0];
            int newCol = j + directions[k][1];

            if(newRow >= 0 && newCol >= 0 && newRow < m && newCol < n && !visited[newRow][newCol] && grid[newRow][newCol] == 1)
            {
                visited[newRow][newCol] = 1;
                grid[newRow][newCol] = 2;
                traverse(grid, newRow, newCol, m, n, visited);
            }
        }
    }

    int findDistance(vector<vector<int>>& grid, int rowStart, int colStart, int m, int n)
    {
        cout<<rowStart<<" "<<colStart<<endl;
        int level = 0;
        vector<vector<int>>visited(m+1, vector<int>(n+1,0));
        
        while(!q.empty())
        {
            int size = q.size();
            for(int i=0;i<size;i++)
            {
                auto front = q.front();
                int row = front.first;
                int col = front.second;
                cout<<"r"<<row<<" "<<"c"<<col<<endl;
                q.pop();
                for(int k=0;k<4;k++)
                {
                    int newRow = row + directions[k][0];
                    int newCol = col + directions[k][1];
                    if(newRow >= 0 && newCol >= 0 && newRow < m && newCol < n && !visited[newRow][newCol])
                    {
                        if(grid[newRow][newCol] == 1)
                            return level;
                        visited[newRow][newCol] = 1;
                        q.push(make_pair(newRow, newCol));
                    }
                }
            }
            level++;
        }

        return level;
    }
};
26575