Given m x n
a matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the VO Lume of water it is able to trap after raining.
Note:
Both m and N is less than 110. The height of each unit cell is greater than 0 and was less than 20,000.
Example:
Given the following 3x6 height map:[ [1,4,3,1,3,2], [3,2,1,3,2,4], [2,3,3,2,3,1]]return 4.
The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
before the rain.
After the rain, water is trapped between the blocks. The total volume of water trapped is 4.
This problem is before that road trapping Rain water expansion, from 2D changed to the big, feel very diao. But in fact, the solution is completely different from the previous one, the problem by the two-dimensional, we can use a double pointer to do, and this three-dimensional, we need to do with BFS, the solution is very ingenious, the following we take the example of the topic to carry out analysis, multi-image warning, mobile traffic party cautious into:
First we should be able to analyze that the bottom of the water can certainly not be on the boundary, because the points on the boundary cannot be closed, then all the points on the boundary can be added to the queue, as the start point of the BFS, and we need a two-dimensional array to mark the points visited, the points we visited are shown in red, then:
We think again, how can be successfully loaded into the water, is not the height of the surrounding should be higher than the current height, a groove to fill the water, and the water capacity depends on the smallest height around, a bit like the feeling of the wood barrel principle, then in order to simulate this method, we use simulated sea level rise method to do, We maintain a sea level altitude of MX, initialized to the minimum, starting from 1 to rise, then we need to go through the BFS traversal from the height of the smallest lattice, then our queue can not use the normal queue, but the use of priority queue, the height of the small team first, the first out, This allows us to traverse three squares with a height of 1, marked with green, as shown in:
To the surrounding BFS search conditions are not out of bounds, and the surrounding lattice is not accessed, then you can see the first and last green lattice above could not be further searched, only the first row in the middle of the green lattice can be searched, there is a gray lattice around it has not been visited, adding it to the queue of priority queues, Then mark it as red, as shown in:
Then the queue with a height of 1 in the priority queues is traversed, when the sea level rises by 1 and becomes 2, we traverse the queue with a height of 2 in the priority queues, and there are 3, as shown in the green tag:
We found that all the squares around the three green lattice have been visited, so without doing anything, the sea level continues to rise to 4, traversing all the squares with a height of 4, as shown in the green tag:
Since we do not specifically declare the height of the same lattice in the priority queue in the order, so should be random, in fact, who first traversed to all the same, the results have no effect, we assume that the first row of the two green lattice to traverse first, then the surrounding each has a gray lattice can traverse, the two gray lattice is lower than the sea level, It is possible to save water and calculate the amount of moisture added to the result res, as shown in:
The two traversed blue squares are added to the queue of priority queues, because their height is small, so the next time you take a lattice from a queue of priority queues, they will be first traversed, then the left side of the blue lattice for BFS search, will traverse to the left of the Gray lattice, because its height is less than sea level, It is also possible to save water and calculate the amount of moisture added to the result res, as shown in:
When the two green lattice traversal is finished, they will be marked red, the blue lattice traversal will be marked red first, and then added to the priority queue, because all the surrounding lattice becomes red, all will not have any action, as shown in:
At this point all the squares are marked red, the sea level continues to rise, continue to traverse the grid in the queue of priority queues, but it will not have any effect on the result, because all the grids have been visited, and then return to res at the end of the loop, see the code below:
classSolution { Public: intTraprainwater (vector<vector<int>>&heightmap) { if(Heightmap.empty ())return 0; intm = Heightmap.size (), n = heightmap[0].size (), res =0, mx =int_min; Priority_queue<pair<int,int, vector<pair<int,int>>, greater<pair<int,int>>>Q; Vector<vector<BOOL>> visited (M, vector<BOOL> (n,false)); Vector<vector<int>> dir{{0,-1},{-1,0},{0,1},{1,0}}; for(inti =0; I < m; ++i) { for(intj =0; J < N; ++j) {if(i = =0|| i = = M-1|| j = =0|| j = = N-1) {Q.push ({heightmap[i][j], I* n +J}); VISITED[I][J]=true; } } } while(!Q.empty ()) {Auto T=q.top (); Q.pop (); intH = t.first, r = t.second/n, c = t.second%N; MX=max (MX, h); for(inti =0; I < dir.size (); ++i) {intx = r + dir[i][0], y = c + dir[i][1]; if(X <0|| X >= m | | Y <0|| Y >= N | | Visited[x][y])Continue; Visited[x][y]=true; if(Heightmap[x][y] < MX) res + = mx-Heightmap[x][y]; Q.push ({heightmap[x][y], x* n +y}); } } returnRes; }};
Similar topics:
Trapping Rain Water
Resources:
Https://discuss.leetcode.com/topic/60914/concise-c-priority_queue-solution
Leetcode all in one topic summary (continuous update ...)
[Leetcode] Trapping Rain Water II collection of rainwater two