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