-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10067.go
73 lines (65 loc) · 1.4 KB
/
10067.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
// UVa 10067 - Playing with Wheels
package main
import (
"fmt"
"os"
)
type config struct {
n [4]int
steps int
}
var (
forbidden map[[4]int]bool
target config
directions = [2]int{-1, 1}
)
func rotate(n [4]int, i, step int) [4]int {
switch n[i] += step; n[i] {
case -1:
n[i] = 9
case 10:
n[i] = 0
}
return n
}
func bfs(initial config) int {
for visited, queue := map[[4]int]bool{initial.n: true}, []config{initial}; len(queue) > 0; queue = queue[1:] {
curr := queue[0]
if curr.n == target.n {
return curr.steps
}
for i := range curr.n {
for _, direction := range directions {
next := rotate(curr.n, i, direction)
if !visited[next] && !forbidden[next] {
visited[next] = true
queue = append(queue, config{next, curr.steps + 1})
}
}
}
}
return -1
}
func main() {
in, _ := os.Open("10067.in")
defer in.Close()
out, _ := os.Create("10067.out")
defer out.Close()
var kase, f int
var t [4]int
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
fmt.Fscanf(in, "%d%d%d%d", &t[0], &t[1], &t[2], &t[3])
initial := config{t, 0}
fmt.Fscanf(in, "%d%d%d%d", &t[0], &t[1], &t[2], &t[3])
target = config{t, -1}
forbidden = make(map[[4]int]bool)
for fmt.Fscanf(in, "%d", &f); f > 0; f-- {
fmt.Fscanf(in, "%d%d%d%d", &t[0], &t[1], &t[2], &t[3])
forbidden[t] = true
}
fmt.Fprintln(out, bfs(initial))
if kase > 1 {
fmt.Fscanln(in)
}
}
}