5-17 汉诺塔的非递归实现 (25分)

5-17 汉诺塔的非递归实现 (25分)

借助堆栈以非递归(循环)方式求解汉诺塔的问题(n, a, b, c),即将N个盘子从起始柱(标记为“a”)通过借助柱(标记为“b”)移动到目标柱(标记为“c”),并保证每个移动符合汉诺塔问题的要求。
输入格式:

输入为一个正整数N,即起始柱上的盘数。
输出格式:

每个操作(移动)占一行,按柱1 -> 柱2的格式输出。
输入样例:

3

输出样例:

a -> c
a -> b
c -> b
a -> c
b -> a
b -> c
a -> c

思路
参考:http://blog.csdn.net/computerme/article/details/18080511
目前只看懂是用堆栈来替代递归中的数据存储问题。所谓的顺时针、逆时针还没搞懂。不过还是有非常多的资源可以参考学习,这道题先贴在这里,找时间继续钻研。

点击访问 PTA-测验

#include<stdio.h>
#define MAX 64
/* 评测结果
时间  结果  得分  题目  编译器     用时(ms)  内存(MB)  用户
2016-09-03 19:49    答案正确    25  5-17    gcc     169     8   569985011
测试点结果
测试点     结果  得分/满分   用时(ms)  内存(MB)
测试点1    答案正确    15/15   1   1
测试点2    答案正确    2/2     1   1
测试点3    答案正确    2/2     2   1
测试点4    答案正确    6/6     169     8
查看代码*/
typedef struct st {
    int s[MAX];
    int top;
    char name;
}*ST;

long Pow(int x,int y);//计算x^y
void Creat(struct st ta[],int n);//初始化
void Hannuota(struct st ta[],long max);//主函数
int Top(ST a) ;
int Pop(ST a) ;
void Push(ST a,int x);

int main() {
    int n;
    scanf("%d",&n);
    struct st ta[3];
    Creat(ta,n);
    long max=Pow(2,n)-1;
//  printf("%lld\n",max);
    Hannuota(ta,max);
    return 0;
}
int Top(ST a) {
    return a->s[a->top];
};
int Pop(ST a) {
    return a->s[a->top--];
};
void Push(ST a,int x) {
    a->s[++a->top]=x;
};

long Pow(int x,int y) {
    long a=(int)x;
    for(int i=1; i<y; i++)a*=x;
    return a;
}


void Creat(struct st ta[],int n) {
    ta[0].name='a';
    ta[0].top=n-1;
//把所有圆盘从大到小放到柱子A上
    for(int i=0; i<n; i++)ta[0].s[i]=n-i;
    ta[1].top=ta[2].top=0;
    for(int i=0; i<n; i++)ta[1].s[i]=ta[2].s[i]=0;
//若n为偶数,按顺时针方向摆放ABC,否则逆时针
    if(n%2==0) {
        ta[1].name='b';
        ta[2].name='c';
    } else {
        ta[1].name='c';
        ta[2].name='b';
    }
}

void Hannuota(struct st ta[],long max) {
    for(int k=0,i=0; k<max; k++) {
        //按顺时针方向吧圆盘1从现在的位置移动到下一根柱子
        Push(&ta[(i+1)%3],Pop(&ta[(i%3)]));
//      ta[(i+1)%3].Push(ta[i%3].Pop());
        printf("%c -> %c\n",ta[i%3].name,ta[(i+1)%3].name);
        i++;
        //把另外两根柱子上的可以移动的圆盘移动到新的柱子上
        if(k++<max) {
            //吧非空柱子上的圆盘移动到空柱子上,当两根柱子都为空时,移动较小的圆盘
            if(Top(&ta[(i+1)%3])==0||
                    Top(&ta[(i-1)%3])>0&&
                    Top(&ta[(i+1)%3]) >Top(&ta[(i-1)%3])) {
                Push(&ta[(i+1)%3],Pop(&ta[(i-1)%3]));
//              ta[(i+1)%3].Push(ta[(i-1)%3].Pop());
                printf("%c -> %c\n",ta[(i-1)%3].name,ta[(i+1)%3].name);
            } else {
                Push(&ta[(i-1)%3],Pop(&ta[(i+1)%3]));
//              ta[(i-1)%3].Push(ta[(i+1)%3].Pop());
                printf("%c -> %c\n",ta[(i+1)%3].name,ta[(i-1)%3].name);
            }
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值