<p>You are given an <code>n x n</code> binary matrix <code>grid</code>. You are allowed to change <strong>at most one</strong> <code>0</code> to be <code>1</code>.</p>

<p>Return <em>the size of the largest <strong>island</strong> in</em> <code>grid</code> <em>after applying this operation</em>.</p>

<p>An <strong>island</strong> is a 4-directionally connected group of <code>1</code>s.</p>

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

<pre>
<strong>Input:</strong> grid = [[1,0],[0,1]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
</pre>

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

<pre>
<strong>Input:</strong> grid = [[1,1],[1,0]]
<strong>Output:</strong> 4
<strong>Explanation: </strong>Change the 0 to 1 and make the island bigger, only one island with area = 4.</pre>

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

<pre>
<strong>Input:</strong> grid = [[1,1],[1,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Can&#39;t change any 0 to 1, only one island with area = 4.
</pre>

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

<ul>
	<li><code>n == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 500</code></li>
	<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

class Solution {
public:
    int largestIsland(vector<vector<int>>& grid) {
        // init
        height_ = grid.size();
        width_ = grid[0].size();
        row_inc_vec_ = {-1, 1, 0, 0};
        col_inc_vec_ = {0, 0, -1, 1};
        unordered_map<int, int> map_pixel_to_island_id;
        unordered_map<int, int> map_island_id_to_size;
        int max_island_size = 0;

        // mark islands
        int island_id = 0;
        vector<vector<bool>> visited(height_, vector<bool>(width_, false));
        for (int i = 0; i < height_; i++) {
            for (int j = 0; j < width_; j++) {
                if (grid[i][j] == 1) {
                    int local_island_size = 0;
                    SearchIsland(grid, i, j, visited, local_island_size, 
                        island_id, map_pixel_to_island_id);
                    map_island_id_to_size[island_id] = local_island_size;
                    max_island_size = max(max_island_size, local_island_size);
                    island_id++;
                }
            }
        }
 
        // search flips
        for (int i = 0; i < height_; i++) {
            for (int j = 0; j < width_; j++) {
                if (grid[i][j] == 0) {
                    int local_island_size = 1;
                    unordered_set<int> island_ids;
                    for (int k = 0; k < row_inc_vec_.size(); k++) {
                        int row_new_index = i + row_inc_vec_[k];
                        int col_new_index = j + col_inc_vec_[k];
                        if ((row_new_index < 0) || (row_new_index >= height_) || 
                            (col_new_index < 0) || (col_new_index >= width_)) {
                            continue;
                        }
                        if (grid[row_new_index][col_new_index] == 0) {
                            continue;
                        }
                        island_ids.insert(map_pixel_to_island_id[GetPixelId(row_new_index, col_new_index)]);
                    }
                    for (auto island_id : island_ids) {
                        local_island_size += map_island_id_to_size[island_id];
                    }
                    max_island_size = max(max_island_size, local_island_size);
                }
            }
        }
        return max_island_size;
    }

    void SearchIsland(const vector<vector<int>>& grid, int i, int j, 
        vector<vector<bool>>& visited, int& local_island_size, int island_id, 
        unordered_map<int, int>& map_pixel_to_island_id) {
        if ((i < 0) || (i >= height_) || (j < 0) || (j >= width_)) {
            return;
        }
        if ((grid[i][j] == 0) || visited[i][j]) {
            return;
        }
        local_island_size++;
        visited[i][j] = true;
        map_pixel_to_island_id[GetPixelId(i, j)] = island_id;
        for (int k = 0; k < row_inc_vec_.size(); k++) {
            int row_new_index = i + row_inc_vec_[k];
            int col_new_index = j + col_inc_vec_[k];
            SearchIsland(grid, row_new_index, col_new_index, visited, local_island_size, island_id, map_pixel_to_island_id);
        }
    }

    inline int GetPixelId(int i, int j) {
        return i * width_ + j;
    }

private:
    int height_;
    int width_;
    vector<int> row_inc_vec_;
    vector<int> col_inc_vec_;
};
202865