【LeetCode 407】Trapping Rain Water II

题意:

存水的升级3维版。这次想要存水必须得四个方向的比当前位置高了。

思路:

边缘的位置肯定不能存水,加入队列。从高度小的点开始搜索,类似水位上升逐渐淹没的过程。下一个位置如果比当前位置高,那肯定也存不了水,直接加入队列。如果比当前位置低,说明下一个点的围墙高度最低就是当前位置的高度了,根据水位和下一个位置的高度计算存水量。

代码:

class Solution {
public:
    struct Node {
        int x;
        int y;
        int h;
        
        Node(int x, int y, int h) {
            this->x = x;
            this->y = y;
            this->h = h;
        }
        
        friend bool operator < (Node a, Node b) {
            return a.h > b.h;
        }
    };
    
    int trapRainWater(vector<vector<int>>& heightMap) {
        if (heightMap.empty()) return 0;
        
        int n = heightMap.size();
        int m = heightMap[0].size();
        
        priority_queue<Node> pque;
        vector<vector<bool>> vis(n, vector<bool>(m, false));
            
        for (int i=0; i<n; ++i) {
            pque.push(Node(i, 0, heightMap[i][0]));
            pque.push(Node(i, m-1, heightMap[i][m-1]));
            vis[i][0] = 1;
            vis[i][m-1] = 1;
        }
        for (int i=0; i<m; ++i) {
            pque.push(Node(0, i, heightMap[0][i]));
            pque.push(Node(n-1, i, heightMap[n-1][i]));
            vis[0][i] = 1;
            vis[n-1][i] = 1;
        }
        
        
        int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        
        int ans = 0;
        int level = 0;
        
        while(!pque.empty()) {
            Node cur = pque.top();
            pque.pop();
            
            int curx = cur.x;
            int cury = cur.y;
            int curh = cur.h;
            level = max(level, curh);
           
            for (int i=0; i<4; ++i) {
                int nxtx = curx + dir[i][0];
                int nxty = cury + dir[i][1];
                if (nxtx < 0 || nxtx >= n || nxty < 0 || nxty >= m || vis[nxtx][nxty]) continue;
                vis[nxtx][nxty] = 1;
                pque.push(Node(nxtx, nxty, heightMap[nxtx][nxty]));
                ans += max(0, level - heightMap[nxtx][nxty]);
            }
        }
        
        return ans;
    }
};

昨天看了好久没看懂。早上看懂了,但是疯狂出bug。
这一周好像都没好好学习,周末我可不休息了。

猜你喜欢

转载自blog.csdn.net/iCode_girl/article/details/93165820