407. Trapping Rain Water II

Source: Internet
Author: User

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 question is trapping Rain water variant, began and did not think out, looked at the big God's answer only then dawned. First of all, think again trapping rain water is how to do, we use the pointer from the outside to traverse, and this problem method is similar to the topic, is to traverse from the outward, then how to include the boundary? This needs to use the small top heap usage, first define a cell class, storage row,col and height, and then define a visited two-dimensional array, to determine whether to access the cell, and then the boundary of these cells into the small top heap, Because the small top heap automatically according to the function of height sorting from small, first take out is the smaller height of the cell, and then detect the cell around not visited and does not exceed the boundary value of the cell height, if the cell height has not been accessed than the height of the boundary, Then the height difference is the volume of the water storage, otherwise do not save water, all the traversal is the result, the code is as follows:

public class Solution {

public class cell{

int row;

int col;

int height;

Public Cell (int row,int col,int height) {

This.row = row;

This.col = col;

This.height = height;

}

}

public int traprainwater (int[][] heightmap) {

if (heightmap==null| | heightmap.length==0| | Heightmap[0].length==0) return 0;

int m = heightmap.length;

int n = heightmap[0].length;

Boolean[][] visited =new Boolean[m][n];

int sum = 0;

Priorityqueue<cell> PQ = new Priorityqueue<> (1,new comparator<cell> () {

public int compare (Cell A,cell b) {

return a.height-b.height;

}

});

for (int i=0;i<m;i++) {

Visited[i][0] = true;

Visited[i][n-1] = true;

Pq.offer (New Cell (i,0,heightmap[i][0));

Pq.offer (New Cell (i,n-1,heightmap[i][n-1));

}

for (int i=0;i<n;i++) {

visited[0][i]= true;

Visited[m-1][i] = true;

Pq.offer (New Cell (0,i,heightmap[0][i));

Pq.offer (New Cell (m-1,i,heightmap[m-1][i));

}

int[][] dirs = new int[][]{{0,1},{0,-1},{-1,0},{1,0}};

while (!pq.isempty ()) {

Cell cell = Pq.poll ();

For (int[] dir:dirs) {

int row = cell.row+dir[0];

int col = cell.col+dir[1];

if (Row>=0&&row<m&&col>=0&&col<n&&!visited[row][col]) {

Visited[row][col] = true;

Sum+=math.max (0,cell.height-heightmap[row][col]);

Pq.offer (New Cell (Row,col,math.max (Cell.height,heightmap[row][col)));

}

}

}

return sum;

}

}

407. Trapping Rain Water II

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.