-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path291.go
54 lines (47 loc) · 951 Bytes
/
291.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
// UVa 291 - The House Of Santa Claus
package main
import (
"fmt"
"io"
"os"
)
var (
out io.WriteCloser
matrix [][]bool
)
func buildMatrix() [][]bool {
adjacency := [][]int{{}, {2, 3, 5}, {3, 5}, {4, 5}, {5}, {}}
matrix := make([][]bool, len(adjacency))
for i := range adjacency {
matrix[i] = make([]bool, len(adjacency))
}
for i := range adjacency {
for _, v := range adjacency[i] {
matrix[i][v], matrix[v][i] = true, true
}
}
return matrix
}
func backtracking(steps []int) {
if len(steps) == 9 {
for _, v := range steps {
fmt.Fprint(out, v)
}
fmt.Fprintln(out)
return
}
curr := steps[len(steps)-1]
for i := 1; i <= 5; i++ {
if matrix[curr][i] {
matrix[curr][i], matrix[i][curr] = false, false
backtracking(append(steps, i))
matrix[curr][i], matrix[i][curr] = true, true
}
}
}
func main() {
out, _ = os.Create("291.out")
defer out.Close()
matrix = buildMatrix()
backtracking([]int{1})
}