Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Archives
Today
Total
관리 메뉴

이곳저곳 관심이 많아요

2장. 순환/하노이탑 본문

Programming/Data structure

2장. 순환/하노이탑

킹수맨 2021. 3. 18. 22:42

 

#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