-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10012.go
69 lines (61 loc) · 1.25 KB
/
10012.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
// UVa 10012 - How Big Is It?
package main
import (
"fmt"
"math"
"os"
)
var (
m int
length float64
visited []bool
radius []float64
)
func currentPos(x, y, r float64) float64 { return x + math.Sqrt((y+r)*(y+r)-(y-r)*(y-r)) }
func calculate(order []int) {
pos := make([]float64, m)
pos[0] = radius[order[0]]
maxLength := 2 * radius[order[0]]
for i := 1; i < m; i++ {
maxPos := radius[order[i]]
for j := 0; j < i; j++ {
maxPos = max(maxPos, currentPos(pos[j], radius[order[j]], radius[order[i]]))
}
pos[i] = maxPos
maxLength = max(maxLength, pos[i]+radius[order[i]])
}
if maxLength < length {
length = maxLength
}
}
func dfs(level int, order []int) {
if level == m {
calculate(order)
return
}
for i := range radius {
if !visited[i] {
visited[i] = true
dfs(level+1, append(order, i))
visited[i] = false
}
}
}
func main() {
in, _ := os.Open("10012.in")
defer in.Close()
out, _ := os.Create("10012.out")
defer out.Close()
var n int
for fmt.Fscanf(in, "%d", &n); n > 0; n-- {
fmt.Fscanf(in, "%d", &m)
radius = make([]float64, m)
for i := range radius {
fmt.Fscanf(in, "%f", &radius[i])
}
visited = make([]bool, m)
length = math.MaxFloat64
dfs(0, nil)
fmt.Fprintf(out, "%.3f\n", length)
}
}