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

/**
    understanding
        can change at most 1 water square to island to make the biggest possible island

    approach 1
        (1) scan for all islands, marking each as island # and noting the size
        (2) init largest from scan. if none, return 1
        (3) scan all water, flip to island and determine size of island made from itself + adjacent unique islands
        (4) return max

    scanning islands
        if reach island square
            start traversal from that square, setting # and hitting all adjacent squares that are
            unvisited island
            add up all island visited in each direction + curr and return result
    
    scanning water
        for each water square
            find all adjacent island squares by consulting grid
            add their sizes + 1
            update max

*/

class Solution {
    constexpr static int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    int R = 0;
    int C = 0;
    bool inbounds(vector<vector<int>>& grid, int r, int c) {
        return r >= 0 && r < R && c >= 0 && c < C;
    }

    int scanIsland(vector<vector<int>>& grid, int r, int c, int id) {
        int sum { 1 };
        grid[r][c] = id;
        int newR, newC;
        for (auto& [rDir, cDir] : dirs) {
            newR = r + rDir;
            newC = c + cDir;
            if (inbounds(grid, newR, newC) && grid[newR][newC] == 1) {
                sum += scanIsland(grid, newR, newC, id);
            }
        }
        return sum;
    }

    void findIslands(vector<vector<int>>& grid, unordered_map<int, int>& islandSize) {
        int islandId = 2;
        for (int r = 0; r < R; ++r) {
            for (int c = 0; c < C; ++c) {
                if (grid[r][c] == 1) {
                    const int size = scanIsland(grid, r, c, islandId);
                    islandSize.insert(make_pair(islandId, size));
                    ++islandId;
                }
            }
        }
    }

    int findLargest(vector<vector<int>>& grid, unordered_map<int, int>& islandSize) {
        unordered_set<int> islands;
        int largest = { 0 };
        int newR, newC, curr;
        for (int r = 0; r < R; ++r) {
            for (int c = 0; c < C; ++c) {
                if (grid[r][c] == 0) {
                    curr = 1;
                    islands.clear();
                    for (auto& [rDir, cDir] : dirs) {
                        newR = r + rDir;
                        newC = c + cDir;
                        if (inbounds(grid, newR, newC) && grid[newR][newC] != 0) {
                            islands.insert(grid[newR][newC]);
                        }
                    }
                    for (auto island : islands) {
                        curr += islandSize[island];
                    }
                }
                largest = std::max(largest, curr);
            }
        }

        if (largest == 0) {
            for (auto& [i, s] : islandSize) {
                largest += s;
            }
        }
        return largest;
    }

public:
    int largestIsland(vector<vector<int>>& grid) {
        R = grid.size();
        C = grid[0].size();
        unordered_map<int, int> islandSize;
        findIslands(grid, islandSize);

        if (islandSize.empty()) return 1;

        return findLargest(grid, islandSize);
    }
};
82378