[알고리즘] 하노이의 탑
/*
하노이의 탑
http://ko.wikipedia.org/wiki/%ED%95%98%EB%85%B8%EC%9D%B4%EC%9D%98_%ED%83%91
1. 한번에 하나만 이동.(모든 원반을 마지막 기둥으로.)
function move($from, $to) {
printf("<br />Move from %d to %d", $from, $to);
}
function hanoi($n, $from, $by, $to) {
if ($n == 1)
move($from, $to);
else {
hanoi($n-1, $from, $to, $by); // 1
move($from, $to); // 2
hanoi($n-1, $by, $from, $to); // 3
}
}
$height = 3;
printf("Height of HANOI tower is %d <br />",$height);
hanoi($height, 1, 2, 3);
/* output
Height of HANOI tower is 3
Move from 1 to 3
Move from 1 to 2
Move from 3 to 2
Move from 1 to 3
Move from 2 to 1
Move from 2 to 3
Move from 1 to 3
*/