|
| 1 | +import "container/heap" |
| 2 | +func min(a, b int) int { |
| 3 | + if a < b { |
| 4 | + return a |
| 5 | + } |
| 6 | + return b |
| 7 | +} |
| 8 | + |
| 9 | +type Item struct { |
| 10 | + pre, x, y int |
| 11 | +} |
| 12 | + |
| 13 | +type Heap []*Item |
| 14 | + |
| 15 | +func (h Heap) Len() int { |
| 16 | + return len(h) |
| 17 | +} |
| 18 | + |
| 19 | +func (h Heap) Less(i, j int) bool { |
| 20 | + return h[i].pre > h[j].pre |
| 21 | +} |
| 22 | + |
| 23 | +func (h Heap) Swap(i, j int) { |
| 24 | + h[i], h[j] = h[j], h[i] |
| 25 | +} |
| 26 | + |
| 27 | +func (h *Heap) Pop() interface{} { |
| 28 | + old := *h |
| 29 | + n := len(old) |
| 30 | + item := old[n - 1] |
| 31 | + *h = old[0:n - 1] |
| 32 | + return item |
| 33 | +} |
| 34 | + |
| 35 | +func (h *Heap) Push(x interface{}) { |
| 36 | + *h = append(*h, x.(*Item)) |
| 37 | +} |
| 38 | + |
| 39 | +func maximumMinimumPath(A [][]int) int { |
| 40 | + r, c := len(A), len(A[0]) |
| 41 | + dire := [4][2]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}} |
| 42 | + vi := make([][]int, r) |
| 43 | + for i := 0; i < r; i++ { |
| 44 | + vi[i] = make([]int, c) |
| 45 | + } |
| 46 | + vi[0][0] = 1 |
| 47 | + |
| 48 | + q := &Heap{&Item{A[0][0], 0, 0}} |
| 49 | + heap.Init(q) |
| 50 | + for q.Len() > 0 { |
| 51 | + it := heap.Pop(q).(*Item) |
| 52 | + if it.x == r-1 && it.y == c-1 { |
| 53 | + return it.pre |
| 54 | + } |
| 55 | + for _, v := range dire { |
| 56 | + nx, ny := it.x + v[0], it.y + v[1] |
| 57 | + if nx >= 0 && nx < r && ny >= 0 && ny < c && vi[nx][ny] == 0 { |
| 58 | + vi[nx][ny] = 1 |
| 59 | + heap.Push(q, &Item{min(it.pre, A[nx][ny]), nx, ny}) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + return 0 |
| 64 | +} |
0 commit comments