-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.go
74 lines (68 loc) · 1.36 KB
/
18.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
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"flag"
"os"
"bufio"
"strings"
"strconv"
)
var pdata = flag.String("data", "", "input data")
var pnum = flag.Uint("num", uint(0), "input data height")
func main() {
flag.Parse()
bt := NewBTree(*pnum)
f, err := os.Open(*pdata)
if err != nil {
fmt.Println("failed to open file ", *pdata, err)
return
}
defer f.Close()
s := bufio.NewScanner(f)
cnt := uint(0)
for s.Scan() {
line := s.Text()
if len(line) == 0 {
continue
}
l := strings.Split(line, " ")
nl := make([]int, len(l))
for i, s := range l {
tmp, err2 := strconv.Atoi(s)
if err2 != nil {
fmt.Println("err line2: ", line)
return
}
nl[i] = tmp
}
bt.SetLine(cnt, nl)
cnt ++
}
fmt.Print("input \n" , *pdata)
last := 0
for i:= *pnum - 1; i >= uint(0); i-- {
lv := 0
rv := 0
for j:= uint(0); j <= i; j ++ {
//fmt.Println(" process " , i, " ", j)
max := 0
if i != *pnum - 1 {
if j == 0 {
_, lv = bt.GetLChild(i,j)
} else {
lv = rv
}
_, rv = bt.GetRChild(i,j)
max = lv
if max < rv { max = rv }
}
last = max + bt.GetK(i,j)
bt.SetV(i,j, last)
}
if i == 0 {
fmt.Println("the answer is : " , last)
break
}
}
//fmt.Print("output \n" , bt)
}