JAVA程序设计:接雨水 II(LeetCode:407)

给定一个 m x n 的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水。

 

说明:

m 和 n 都是小于110的整数。每一个单位的高度都大于 0 且小于 20000。

 

示例:

给出如下 3x6 的高度图:
[
  [1,4,3,1,3,2],
  [3,2,1,3,2,4],
  [2,3,3,2,3,1]
]

返回 4。

思路:我们可以采用优先队列的做法,其中优先队列按照高度升序排序,将所有边界点加进优先队列,然后我们从队头进行广搜,并标记所有访问过的点,每次将当前位置能放水的最大高度加入答案即可。

class Solution {
	class Cell{
		private int row;
		private int col;
		private 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<=2 || heightMap.length<=2)
        	return 0;
        PriorityQueue queue=new PriorityQueue<>(Comparator.comparingInt((Cell cell)->cell.height));
        int m=heightMap.length;
        int n=heightMap[0].length;
        boolean[][] visited=new boolean[m][n];
        for(int i=0;i=0 && row=0 && col

 

你可能感兴趣的:(JAVA程序设计:接雨水 II(LeetCode:407))