分享

汉诺塔

 baijeg 2011-01-11
#include <stdio.h>

//移动步骤计数
static int number = 0;

/*
* 借助于x,从from移动n个到to
*
* n 要移动的个数
* from 初始的位置
* to 移动之后的文件
* x 需要借助的位置
*/
void hanluota(int n, char from, char to, char x)
{
if(n <= 1)
{
printf("%d : %c --> %c\n", number++, from, to);
return;
}

//把from上的n-1个移动到x暂存
hanluota(n-1, from, x, to);
//把from最下面的一个移动到to
printf("%d : %c --> %c\n", number++, from, to);
//把x上的n-1个移动到to
hanluota(n-1, x, to, from);
}

int main(void)
{
hanluota(3, 'a', 'b', 'c');
printf("finished");
getchar();
return 0;
}


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多