407. Trapping Rain Water II

Source: Internet
Author: User

Given a m x n matrix of positive integers representing the height of each unit cellinchA 2D elevation map, compute the volume of water it isable to trap after raining. Note:both m and N is less than the. The height of each unit cell isGreater than0and isLess than -, the. 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]]return4. 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.

Analysis, according to the principle of the cask, first find the periphery of the shortest bar, inside if there is a bar than it is shorter, must be able to save water (because four weeks all bar is higher than it)

Note that it is possible to save more water, as it is likely that the cell will change in height. So to put the high bar in the middle of the BFS into the queue, as the water level rises to the height of these bars to see if there is a groove to save more

44-45 Lines of Logic is

if (Height[row][col] < cur) {

Res + = Cur.height-height[row][col];

Queue.offer (New Cell (Row, col, Cur.height));

}

else {

Queue.offer (New Cell (Row, col, Height[row][col]));

}

public class Solution {The 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[][] heights) {if (heights = = NULL | | heights.length = 0 | | heights[0].leng        th = = 0) return 0; priorityqueue<cell> queue = new Priorityqueue<> (1, New comparator<cell> () {public int compare (Cell A, cell B)            {return a.height-b.height;                }        });        int m = heights.length;        int n = heights[0].length;        Boolean[][] visited = new Boolean[m][n];        Initially, add all the Cells which is on the borders to the queue.            for (int i = 0; i < m; i++) {visited[i][0] = true;            Visited[i][n-1] = true;            Queue.offer (New Cell (i, 0, heights[i][0])); Queue.offer (New CelL (i, n-1, heights[i][n-1]));            } for (int i = 0; i < n; i++) {visited[0][i] = true;            Visited[m-1][i] = true;            Queue.offer (New Cell (0, I, heights[0][i]));        Queue.offer (New Cell (M-1, I, heights[m-1][i])); }//From the borders, pick the shortest cell visited and check it neighbors://If the neighbor is shorter , collect the water it can trap and update its height as its height plus the water trapped//Add all its neighbors        to the queue.        int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0,-1}, {0, 1}};        int res = 0;            while (!queue.isempty ()) {Cell cell = Queue.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;              Res + = Math.max (0, Cell.height-heights[row][col]);                Queue.offer (New Cell (Row, col, Math.max (Heights[row][col), cell.height));    }}} return res; }}

  

407. Trapping Rain Water II

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.