Skip to content

Instantly share code, notes, and snippets.

@hiroshiro
Created March 18, 2015 10:43
Show Gist options
  • Save hiroshiro/5ddff067fd54ea8f2c21 to your computer and use it in GitHub Desktop.
Save hiroshiro/5ddff067fd54ea8f2c21 to your computer and use it in GitHub Desktop.
再帰と後戻りアルゴリズム ハノイの塔 CポインタRichard Reeseオライリー・ジャパン
#include <stdio.h>
void TowersOfHanoi();
int main()
{
int n = 3;
char a = 'a';
char b = 'b';
char c = 'c';
TowersOfHanoi(n, a, b, c);
}
void TowersOfHanoi(int n, char frompeg, char topeg, char auxpeg) {
/* 1枚の円盤なら、移して反る */
printf("Move disk 1 from geg %c to peg %c", frompeg, topeg);
return;
/* 上から n-1枚の円盤をAからBへ、Cを中間に使う */
TowersOfHanoi(n-1, frompeg, auxpeg, topeg);
/* 残りの円盤をAからCへ */
printf("\nMove disk %d form peg %c to peg %c\n", n, frompeg, topeg);
/* 上からn-1枚の円盤をBからCへ、Aを中間に使う */
TowersOfHanoi(n-1, auxpeg, topeg, frompeg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment