<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:
    int shortestBridge(vector<vector<int>>& grid) {
        int n = grid.size();

        vector<vector<int>> a;
        // vector<vector<int>> b;

        bool first = true;

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                if(grid[i][j] == 1) {
                    if(first) {
                        infect(i, j, grid, a);
                        first = false;
                    }
                    /* 
                    else {
                        infect(i, j, grid, b);
                    }
                    */
                }
            }
        }

        for(int i = 0; i < a.size(); i++) {
            std::cout << "[" << a[i][0] << "," << a[i][1] << "]" << std::endl;
        }
/*
        std::cout << "" << std::endl;

        for(int i = 0; i < b.size(); i++) {
            std::cout << "[" << b[i][0] << "," << b[i][1] << "]" << std::endl;
        }
*/
        int ans = 0;

        vector<vector<int>> b;

        while(true) {
            bool stop = expand(grid, a, b);

            if(stop) {
                break;
            }

            ans += 1;

            a.clear();

            for(int i = 0; i < b.size(); i++) {
                a.push_back(b[i]);
            }

            b.clear();
        }

        return ans;
    }

    bool expand(vector<vector<int>>& g, vector<vector<int>>& a, vector<vector<int>>& b) {
        for(int i = 0; i < a.size(); i++) {
            int x = a[i][0];
            int y = a[i][1];

            if(valid(x - 1, y, g)) {
                if(g[x - 1][y] == 0) {
                    g[x - 1][y] = 2;

                    vector<int> v;

                    v.push_back(x - 1);
                    v.push_back(y);

                    b.push_back(v);
                } else if(g[x - 1][y] == 1) {
                    return true;
                }
            }

            if(valid(x + 1, y, g)) {
                if(g[x + 1][y] == 0) {
                    g[x + 1][y] = 2;

                    vector<int> v;

                    v.push_back(x + 1);
                    v.push_back(y);

                    b.push_back(v);
                } else if(g[x + 1][y] == 1) {
                    return true;
                }
            }

            if(valid(x, y - 1, g)) {
                if(g[x][y - 1] == 0) {
                    g[x][y - 1] = 2;

                    vector<int> v;

                    v.push_back(x);
                    v.push_back(y - 1);

                    b.push_back(v);
                } else if(g[x][y - 1] == 1) {
                    return true;
                }
            }

            if(valid(x, y + 1, g)) {
                if(g[x][y + 1] == 0) {
                    g[x][y + 1] = 2;

                    vector<int> v;

                    v.push_back(x);
                    v.push_back(y + 1);

                    b.push_back(v);
                } else if(g[x][y + 1] == 1) {
                    return true;
                }
            }
        }

        return false;
    }

    void infect(int i, int j, vector<vector<int>>& g, vector<vector<int>>& a) {
        g[i][j] = 2;

        vector<int> v;

        v.push_back(i);
        v.push_back(j);

        a.push_back(v);

        if(valid(i - 1, j, g) && g[i - 1][j] == 1) {
            infect(i - 1, j, g, a);
        }

        if(valid(i + 1, j, g) && g[i + 1][j] == 1) {
            infect(i + 1, j, g, a);
        }

        if(valid(i, j - 1, g) && g[i][j - 1] == 1) {
            infect(i, j - 1, g, a);
        }

        if(valid(i, j + 1, g) && g[i][j + 1] == 1) {
            infect(i, j + 1, g, a);
        }
    }

    bool valid(int i, int j, vector<vector<int>>& g) {
        if(i < 0 || i >= g.size()) {
            return false;
        }

        if(j < 0 || j >= g.size()) {
            return false;
        }

        return true;
    }
};

/*
[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]
*/
52075