-
Notifications
You must be signed in to change notification settings - Fork 27
/
647.go
79 lines (74 loc) · 1.56 KB
/
647.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
75
76
77
78
79
// UVa 647 - Chutes and Ladders
package main
import (
"fmt"
"os"
)
func solve(throws []int, players int, ladders map[int]int, loseTurns, extraTurns map[int]bool) int {
squares := make([]int, players)
loseTurn := make(map[int]bool)
for i, player := 0, 0; i < len(throws); {
if loseTurn[player] {
loseTurn[player] = false
player = (player + 1) % players
continue
}
if squares[player] >= 95 && squares[player]+throws[i] > 100 {
player = (player + 1) % players
i++
continue
}
if squares[player] += throws[i]; squares[player] == 100 {
return player + 1
}
i++
if s, ok := ladders[squares[player]]; ok {
squares[player] = s
}
if loseTurns[squares[player]] {
loseTurn[player] = true
}
if !extraTurns[squares[player]] {
player = (player + 1) % players
}
}
return -1
}
func main() {
in, _ := os.Open("647.in")
defer in.Close()
out, _ := os.Create("647.out")
defer out.Close()
var tmp, players, s1, s2 int
var throws []int
for {
if fmt.Fscanf(in, "%d", &tmp); tmp == 0 {
break
}
throws = append(throws, tmp)
}
for {
if fmt.Fscanf(in, "%d", &players); players == 0 {
break
}
ladders := make(map[int]int)
for {
if fmt.Fscanf(in, "%d%d", &s1, &s2); s1 == 0 && s2 == 0 {
break
}
ladders[s1] = s2
}
loseTurns, extraTurns := make(map[int]bool), make(map[int]bool)
for {
if fmt.Fscanf(in, "%d", &tmp); tmp == 0 {
break
}
if tmp < 0 {
loseTurns[-tmp] = true
} else {
extraTurns[tmp] = true
}
}
fmt.Fprintln(out, solve(throws, players, ladders, loseTurns, extraTurns))
}
}