Algorithms
하노이탑
Ohjeonghak
2014. 9. 4. 15:49
반응형
하노이 탑의 디스크 이동 과정 출력하기.
#include<stdio.h> int n = 0; void hanoi(int n, int from, int temp, int to); int main() { scanf("%d", &n); hanoi(n, 1, 2, 3); return 0; } void hanoi(int n, int from, int temp, int to) { if (n == 1) printf("%d -> %d\n", from, to); else { hanoi(n - 1, from, to, temp); printf("%d -> %d\n", from, to); hanoi(n - 1, temp, from, to); } }
반응형