이곳저곳 관심이 많아요
2장. 순환/하노이탑 본문
#include <stdio.h>
//막대 from에 쌓여있는 n개의 원판을 막대 tmp를 사용해 to로 옮긴다.
void hanoi_tower(int n,char from, char tmp, char to) {
if (n == 1) {
printf("원판 1을 %c에서 %c로 옮긴다.\n",from,to);
}
else {
hanoi_tower(n - 1, from, to, tmp);
printf("원판 %d를 %c에서 %c로 옮긴다.\n", n, from, to);
hanoi_tower(n - 1, tmp, from, to);
}
}
int main(void) {
hanoi_tower(4, 'A', 'B', 'C');
}
Comments