이야기박스
C) 하노이의 탑 본문
반응형
자바로 하노이의 탑 UI를 만들었죠.
이제 그 근본이 된 코드.
c로 짠 코드를 올리도록 하겠습니다.
이중재귀를 이용한 하노이의 탑입니다!
#include <stdio.h>
void move(int n, char from, char to) {
// n번째 블록을 from에서 to로 옮겨라
printf("%dth %c %c\n", n, from, to);
}
int hanoi(int n,char from, char other, char to) {
// printf("Checker >> n : %d, from : %c , other : %c, to : %c\n", n, from, other, to);
if (n == 1) {
move(n, from, to);
return 0;
}
hanoi(n - 1, from, to, other);
move(n, from, to);
hanoi(n - 1, other, from, to);
return 0;
}
void main() {
int n;
scanf("%d", &n);
hanoi(n,'A','B','C');
}
자바 게시판에 좀더 자세한 하노이의 탑이 있습니다! ㅎㅎ
좀더 좋은 프로그램으로 돌아올게요.
반응형
'Programming Language > c, c++' 카테고리의 다른 글
| c++) 이진 탐색 트리 (0) | 2017.11.04 |
|---|---|
| c++) 정렬 알고리즘 종합 정리 (0) | 2017.10.22 |
| c) Heap 정렬을 해보자 (0) | 2017.03.17 |
| c) 가중치 / 다익스트라 응용, 최단거리 찾기 + 개수 찾기 (2) | 2017.03.17 |
| c++) 로또 번호 뽑기 (0) | 2017.03.17 |