티스토리 뷰
문제
풀이
package main
import (
"bufio"
"fmt"
"math"
"os"
)
func main() {
var n int
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
fmt.Fscanln(reader, &n)
defer writer.Flush()
fmt.Fprintln(writer, int(math.Pow(2, float64(n)))-1)
hanoi(n, 1, 3, 2, writer)
}
func hanoi(n, from, to, via int, writer *bufio.Writer) {
if n == 1 {
fmt.Fprintf(writer, "%d %d\n", from, to)
return
}
hanoi(n-1, from, via, to, writer)
move(from, to, writer)
hanoi(n-1, via, to, from, writer)
}
func move(from, to int, writer *bufio.Writer) {
fmt.Fprintf(writer, "%d %d\n", from, to)
}
728x90
'dev > algorithm' 카테고리의 다른 글
BOJ / 2231번 / 분해합 [Golang] (0) | 2020.06.22 |
---|---|
BOJ / 2798번 / 블랙잭 [Golang] (0) | 2020.06.21 |
BOJ / 2447번 / 별 찍기 - 10 [Golang] (0) | 2020.05.19 |
BOJ / 10870번 / 피보나치 수 5 [Golang] (0) | 2020.05.18 |
BOJ / 10872번 / 팩토리얼 [Golang] (0) | 2020.05.17 |
댓글