https://leetcode.com/problems/trapping-rain-water-ii/
Public classSolution {classCell { Public intx; Public inty; Public intvalue; PublicCell (intXintYintvalue) { This. x =x; This. y =y; This. Value =value; } } Public intTraprainwater (int[] heightmap) { intLevel = 0; intresult = 0; if(Heightmap = =NULL|| Heightmap.length = = 0 | | Heightmap[0].length = = 0) { returnresult; } Boolean[] visited =New Boolean[Heightmap.length] [Heightmap[0].length]; int[] dir = {{1, 0}, {-1, 0}, {0, 1}, {0,-1}}; Priorityqueue<Cell> queue =NewPriorityqueue<cell> (NewComparator<cell>() { Public intCompare (cell C1, cell C2) {returnC1.value-C2.value; } }); for(inti = 0; i < heightmap.length; i++) { for(intj = 0; J < Heightmap[0].length; J + +) { if(i = = 0 | | j = 0 | | i = = HEIGHTMAP.LENGTH-1 | | j = heightmap[0].length-1) {Queue.offer (NewCell (i, J, Heightmap[i][j])); VISITED[I][J]=true; } Else{Visited[i][j]=false; } } } while(!Queue.isempty ()) {Cell current=Queue.poll (); intHeight =Current.value; level=Math.max (height, level); for(int[] move:dir) { intx = current.x + move[0]; inty = current.y + move[1]; if(x >= 0 && x < heightmap.length && y >= 0 && y < heightmap[0].length && visite D[x][y] = =false) {result+ = Heightmap[x][y] < level? Level-heightmap[x][y]: 0; Visited[x][y]=true; Queue.offer (NewCell (x, Y, heightmap[x][y])); } } } returnresult; }}
Reference: http://www.cnblogs.com/grandyang/p/5928987.html
[Leetcode] 407. Trapping Rain Water II