leetcode407. Trapping Rain Water II 接雨水2

思路:用优先队列维护一个'圈’(边界),从圈的短板处内溢的过程。广度优先搜索结合优先队列。

class Solution{
    	private class Cell implements Comparable{
    		int row;
    		int col;
    		int height;
			public Cell(int row, int col, int height) {
				this.row = row;
				this.col = col;
				this.height = height;
			}
			@Override
			public int compareTo(Cell o) {
				return this.height- o.height;
			}
    	} 
        public int trapRainWater(int[][] heightMap) {
        	if(heightMap.length<=1 || heightMap[0].length<=1){//<=1行或每行<=1个元素,没法3维接水
        		return 0;
        	}
            boolean[][] visited=new boolean[heightMap.length][heightMap[0].length];//默认被初始化为false
            PriorityQueue queue=new PriorityQueue();//小堆
            int waterTraped=0;
            //1.初始化把最外围圈入队
            for(int j=0;jheightMap.length-1 || col<0 || col>heightMap[0].length-1 || visited[row][col]==true){//越界检查+已经计算检查
            			continue;
            		}
            		waterTraped+=Math.max(lowestWall.height-heightMap[row][col], 0);//当前单元格高



你可能感兴趣的:(leetcode)