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

class Solution {
public:
    int trapRainWater(vector<vector<int>>& heightMap) {
        int r = heightMap.size();
        int c = heightMap[0].size();
        int added[r][c];
        int debug[r][c];
        memset(added,0,sizeof(added));
        priority_queue<pair<int,pair<int,int>>> bound;
        for(int i=0;i<r;i++) {
            if(i == 0 || i == r-1) {
                for(int j=0;j<c;j++) {
                    bound.push({-heightMap[i][j],{i,j}});
                    added[i][j] = 1;
                    debug[i][j] = 0;
                }
            } else {
                bound.push({-heightMap[i][0],{i,0}});
                added[i][0] = 1;
                debug[i][0] = 0;
                bound.push({-heightMap[i][c-1],{i,c-1}});
                added[i][c-1] = 1;
                debug[i][c-1] = 0;
                
            }
        }
        int res = 0;
        while(!bound.empty()) {
            auto p = bound.top();
            bound.pop();
            int h = -p.first;
            int i = p.second.first;
            int j = p.second.second;
            cout << h <<" " << i << " " << j <<"\n";
            vector<pair<int,int>> temp;
            if(i > 0) {
                temp.push_back({i-1,j});
            }
            if(j > 0) {
                temp.push_back({i,j-1});
            }
            if(i < r-1) {
                temp.push_back({i+1,j});
            }
            if(j <c-1 ) {
                temp.push_back({i,j+1});
            }
            for(auto p2 : temp) {
                int x = p2.first;
                int y = p2.second;
                if(!added[x][y]) {
                    added[x][y] = 1;
                    if(heightMap[x][y] < h) {
                    res += h - heightMap[x][y];
                    debug[x][y] = h - heightMap[x][y];
                    bound.push({-h,{x,y}});
                    //cout << "add " << h <<" " << x << "," << y <<"\n";
                    } else {
                        bound.push({-heightMap[x][y],{x,y}});
                        debug[x][y] = 0;
                        //cout << "add " << heightMap[x][y] <<" " << x << "," << y <<"\n";
                    }
                }
            }  
        }
        /*
        for(int i=0;i<r;i++) {
            for(int j=0;j<c;j++) {
                cout << debug[i][j] <<" ";
            }
            cout <<"\n";
        }
        cout <<"\n";
        for(int i=0;i<r;i++) {
            for(int j=0;j<c;j++) {
                cout << heightMap[i][j] <<" ";
            }
            cout <<"\n";
        }
        cout <<"\n";
        for(int i=0;i<r;i++) {
            for(int j=0;j<c;j++) {
                cout << heightMap[i][j] + debug[i][j]<<" ";
            }
            cout <<"\n";
        }
        cout <<"\n";
        */
        return res;
    }
};
32318