プログラミング+α学習ブログ

勉強したことの備忘録です。

ハノイの塔

わからない(ToT)

#include <stdio.h>

#define N 3 // 枚数

/*--- 円盤をx軸からy軸へ移動 ---*/
void move(int no, int x, int y)
{
    if (no > 1)
        move(no - 1, x, 6 - x - y);
    printf("%d%d軸から%d軸へ移動\n", no, x, y);
    if (no > 1)
        move(no - 1, 6 - x - y, y);
}

int main(void)
{
    move(N, 1, 3);
    
    return (0);
}

実行結果

1を1軸から3軸へ移動
2を1軸から2軸へ移動
1を3軸から2軸へ移動
3を1軸から3軸へ移動
1を2軸から1軸へ移動
2を2軸から3軸へ移動
1を1軸から3軸へ移動