C++ Floodfill solution with priority_queue
\nclass Solution {\npublic:\n    int trapRainWater(vector<vector<int>>& heightMap) {\n        int n = heightMap.size();\n        if(n == 0) return 0;\n        i
```\nclass Solution {\npublic:\n    int trapRainWater(vector<vector<int>>& heightMap) {\n        int n = heightMap.size();\n        if(n == 0) return 0;\n        int m = heightMap[0].size();\n        if(m == 0) return 0;\n        int result = 0;\n        vector<vector<bool>> isVisit(n, vector<bool>(m, false));\n        priority_queue<pair<int, pair<int, int>>> myQueue;\n        \n        for(int i = 0; i < n; i++) {\n            for(int j = 0; j < m; j++) {\n                if(i==0 || i == n-1 || j == 0 || j == m-1) {\n                    // we push all the boundary points into the priorrity_queue\n                    isVisit[i][j] = true;\n                    // push negative height as the priority_queue is a big heap\n                    myQueue.push(make_pair(-heightMap[i][j], make_pair(i, j)));\n                }\n            }\n        }\n        const int go_x[4] = {1, -1, 0, 0};\n        const int go_y[4] = {0, 0, 1, -1};\n        while(!myQueue.empty()) {\n            // We always extract the smallest block and try to extend it to its four neighbors.\n            auto temp_front = myQueue.top();\n            myQueue.pop();\n            int temp_height = -temp_front.first;\n            int fx = temp_front.second.first;\n            int fy = temp_front.second.second;\n            for(int d = 0; d < 4; d++) {\n                int tx = fx + go_x[d];\n                int ty = fy + go_y[d];\n                if(tx < 0 || tx >=n || ty < 0 || ty >= m || isVisit[tx][ty]) continue;\n                if(heightMap[tx][ty] < temp_height) {\n                    // if its neightbour's height is smallest than it, we fill water in it.\n                    result += (temp_height - heightMap[tx][ty]);\n                    heightMap[tx][ty] = temp_height;\n                }\n                isVisit[tx][ty] = true;\n                myQueue.push(make_pair(-heightMap[tx][ty], make_pair(tx, ty)));\n            }\n        }\n        return result;\n    }\n};\n```
[]