-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path114.go
72 lines (65 loc) · 1.38 KB
/
114.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
// UVa 114 - Simulation Wizardry
package main
import (
"fmt"
"os"
)
type (
point struct{ x, y int }
bumper struct{ value, cost int }
ball struct {
point
direction, lifetime int
}
)
var (
m, n, costWall int
bumperMap = make(map[point]bumper)
directionMap = map[int][2]int{0: {1, 0}, 1: {0, 1}, 2: {-1, 0}, 3: {0, -1}}
)
func play(b ball) int {
var points int
for b.lifetime > 1 {
b.lifetime--
direction := directionMap[b.direction]
p := point{b.x + direction[0], b.y + direction[1]}
if p.x == 1 || p.y == 1 || p.x == m || p.y == n {
b.lifetime -= costWall
b.direction = (b.direction + 3) % 4
} else {
if bp, ok := bumperMap[p]; ok {
b.lifetime -= bp.cost
points += bp.value
b.direction = (b.direction + 3) % 4
} else {
b.point = p
}
}
}
return points
}
func main() {
in, _ := os.Open("114.in")
defer in.Close()
out, _ := os.Create("114.out")
defer out.Close()
var p, total int
var pt point
var bp bumper
var b ball
fmt.Fscanf(in, "%d%d", &m, &n)
fmt.Fscanf(in, "%d", &costWall)
for fmt.Fscanf(in, "%d", &p); p > 0; p-- {
fmt.Fscanf(in, "%d%d%d%d", &pt.x, &pt.y, &bp.value, &bp.cost)
bumperMap[pt] = bp
}
for {
if _, err := fmt.Fscanf(in, "%d%d%d%d", &b.x, &b.y, &b.direction, &b.lifetime); err != nil {
break
}
points := play(b)
fmt.Fprintln(out, points)
total += points
}
fmt.Fprintln(out, total)
}