-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path168.go
77 lines (68 loc) · 1.33 KB
/
168.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
// UVa 168 - Theseus and the Minotaur
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
var (
maze map[byte][]byte
k int
candles []string
candleMap map[byte]bool
trapped byte
)
func buildMaze(token string) map[byte][]byte {
maze = make(map[byte][]byte)
for _, path := range strings.Split(token, ";") {
t := strings.Split(path, ":")
maze[t[0][0]] = []byte(t[1])
}
return maze
}
func dfs(m, t byte, step int) bool {
if step == k {
candleMap[m] = true
candles = append(candles, string(m))
step = 0
}
done := true
for _, node := range maze[m] {
if node != t && !candleMap[node] {
if dfs(node, m, step+1) {
return true
}
done = false
}
}
if done {
trapped = m
}
return done
}
func solve(minotaur, theseus byte) {
candles = nil
candleMap = make(map[byte]bool)
dfs(minotaur, theseus, 1)
}
func main() {
in, _ := os.Open("168.in")
defer in.Close()
out, _ := os.Create("168.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var line string
var minotaur, theseus byte
for s.Scan() {
if line = s.Text(); line == "#" {
break
}
tokens := strings.Split(line, ". ")
buildMaze(tokens[0])
fmt.Sscanf(tokens[1], "%c %c %d", &minotaur, &theseus, &k)
solve(minotaur, theseus)
fmt.Fprintf(out, "%s /%c\n", strings.Join(candles, " "), trapped)
}
}