-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10017.go
70 lines (62 loc) · 1.09 KB
/
10017.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
// UVa 10017 - The Never Ending Towers of Hanoi
package main
import (
"fmt"
"io"
"os"
)
var (
out io.WriteCloser
step, m int
pegs [][]int
)
func output() {
for i, peg := range pegs {
fmt.Fprintf(out, "%c=>", 'A'+i)
if len(peg) > 0 {
fmt.Fprint(out, " ")
for _, disk := range peg {
fmt.Fprintf(out, " %d", disk)
}
}
fmt.Fprintln(out)
}
fmt.Fprintln(out)
}
func hanoi(n, curr, via, target int) {
if n > 1 {
hanoi(n-1, curr, target, via)
}
pegs[target] = append(pegs[target], pegs[curr][len(pegs[curr])-1])
pegs[curr] = pegs[curr][:len(pegs[curr])-1]
if step++; step > m {
return
}
output()
if n > 1 {
hanoi(n-1, via, curr, target)
}
}
func solve(n int) {
pegs = make([][]int, 3)
for i := n; i > 0; i-- {
pegs[0] = append(pegs[0], i)
}
step = 0
output()
hanoi(n, 0, 1, 2)
}
func main() {
in, _ := os.Open("10017.in")
defer in.Close()
out, _ = os.Create("10017.out")
defer out.Close()
var n int
for kase := 1; ; kase++ {
if fmt.Fscanf(in, "%d%d", &n, &m); n == 0 && m == 0 {
break
}
fmt.Fprintf(out, "Problem #%d\n\n", kase)
solve(n)
}
}