|
| 1 | +publicclassSolution{ |
| 2 | +PriorityQueue<(int,int),int>pq; |
| 3 | +privateHashSet<(int,int)>visited; |
| 4 | +publicintSwimInWater(int[][]grid) |
| 5 | +{ |
| 6 | +intn=grid.Length; |
| 7 | +pq=newPriorityQueue<(int,int),int>(); |
| 8 | +visited=newHashSet<(int,int)>(); |
| 9 | +pq.Enqueue((0,0),grid[0][0]); |
| 10 | +while(pq.TryDequeue(outvarnei,outvarpriority)) |
| 11 | +{ |
| 12 | +intr=nei.Item1; |
| 13 | +intc=nei.Item2; |
| 14 | +visited.Add((r,c)); |
| 15 | + |
| 16 | +if(r==n-1&&c==n-1)returnpriority; |
| 17 | +EnQueue(r+1,c,grid,priority); |
| 18 | +EnQueue(r-1,c,grid,priority); |
| 19 | +EnQueue(r,c+1,grid,priority); |
| 20 | +EnQueue(r,c-1,grid,priority); |
| 21 | +} |
| 22 | +return-1; |
| 23 | +} |
| 24 | + |
| 25 | +privatevoidEnQueue(intr,intc,int[][]grid,intpreCost) |
| 26 | +{ |
| 27 | +if(r>=grid.Length||r<0)return; |
| 28 | +if(c>=grid.Length||c<0)return; |
| 29 | +if(visited.Contains((r,c)))return; |
| 30 | + |
| 31 | +pq.Enqueue((r,c),Math.Max(preCost,grid[r][c])); |
| 32 | +} |
| 33 | +} |