Lintcode364 trapping Rain Water II solution

Source: Internet
Author: User

"Title description"

Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1< /c3>x1, compute how much water it's able to trap after raining.

Given n x m nonnegative integers, which represent two-dimensional elevation maps, the area of each unit is 1 x 1, calculating how much water can be caught after the rain.

"Topic link"

www.lintcode.com/en/problem/trapping-rain-water-ii/

"Problem Analysis"

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,

"Reference Answer"

www.jiuzhang.com/solutions/trapping-rain-water-ii/



Lintcode364 trapping Rain Water II solution

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.