<p>Given an <code>m x n</code> integer matrix <code>heightMap</code> representing the height of each unit cell in a 2D elevation map, return <em>the volume of water it can trap after raining</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/trap1-3d.jpg" style="width: 361px; height: 321px;" />
<pre>
<strong>Input:</strong> heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> After the rain, water is trapped between the blocks.
We have two small ponds 1 and 3 units trapped.
The total volume of water trapped is 4.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/trap2-3d.jpg" style="width: 401px; height: 321px;" />
<pre>
<strong>Input:</strong> heightMap = [[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]]
<strong>Output:</strong> 10
</pre>

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

<ul>
	<li><code>m == heightMap.length</code></li>
	<li><code>n == heightMap[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 200</code></li>
	<li><code>0 &lt;= heightMap[i][j] &lt;= 2 * 10<sup>4</sup></code></li>
</ul>

//given some point, can water flow out from any of the 4 directions?
class Solution {
public:
    int trapRainWater(vector<vector<int>>& heightMap) {
        int rows = heightMap.size();
        int cols = heightMap[0].size();
        vector<vector<bool>> visited(rows, vector<bool>(cols, false));
        set<Trip> min_heap;
        for(int i = 0; i < rows; ++i)
        {
            for(int j = 0; j < cols; ++j)
            {
                if(isOnBoundary(i, j, rows, cols))
                {
                    min_heap.insert({heightMap[i][j], i, j});
                    visited[i][j] = true;
                }
            }
        }
        int total_water_trapped{};

        const int dx[4] = {1, 0, -1, 0};
        const int dy[4] = {0, 1, 0, -1};

        while(!min_heap.empty())
        {
            auto it = min_heap.begin();
            auto [curr_height, x, y] = *it;
            min_heap.erase(it);
            for(int i = 0; i < 4; ++i)
            {
                int new_x = x + dx[i];
                int new_y = y + dy[i];
                if(isValidPoint(new_x, new_y, rows, cols) && !visited[new_x][new_y])
                {
                    if(heightMap[new_x][new_y] < curr_height)
                    {
                        total_water_trapped += curr_height - heightMap[new_x][new_y];
                        heightMap[new_x][new_y] = curr_height;
                    }
                    min_heap.insert({heightMap[new_x][new_y], new_x, new_y});
                    visited[new_x][new_y] = true;
                }
            }
        }
        return total_water_trapped;
    }
private:
    bool isOnBoundary(int x, int y, int rows, int cols)
    {
        return x == 0 || y == 0 || x == (rows - 1) || y == (cols - 1);
    }
    bool isValidPoint(int x, int y, int rows, int cols)
    {
        return x >= 0 && y >= 0 && x < rows && y < cols;
    }
    struct Trip
    {
        int x, y, z;
        friend bool operator<(const Trip& t1, const Trip& t2)
        {
            return tie(t1.x, t1.y, t1.z) < tie(t2.x, t2.y, t2.z);
        }
    };  
};
26381