汉诺塔的非递归实现 (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

用三个盘子的情况进行模拟,结合栈的先进后出的 特点,将先n-1个盘子进行存储入栈,把最底下一个盘子入栈,(注意,只要n==1得时候就会出栈,在入栈的时候会对n的值进行判断),如果n!=1那么就继续按照规则分解,直到分解到n-1个盘子的最下面一个盘子的时候那么n就等于1,直接出栈。其实我觉得哈,和递归差不多,只需注意那个输出的条件,都是不断循环。注意本题的要求是输出所有的步骤,包括前n-1个的移动。

AC代码:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define STACKSIZE 100

typedef struct
{
    int n;
    char a;
    char b;
    char c;
}ElementType;

typedef int Position;
typedef struct snode* PtrToSnode;
struct snode
{
    ElementType* data;
    Position top;
    int maxsize;
};
typedef PtrToSnode mystack;
mystack create(int maxsize)
{
    mystack ms = (mystack)malloc(sizeof(struct snode));
    ms->data = (ElementType*)malloc(maxsize * sizeof(ElementType));
    ms->maxsize = maxsize;
    ms->top = -1;
    return ms;
}

ElementType ERROR;



mystack create(int maxsize);
bool isEmpty(mystack ms);
bool pushdata(mystack ms, ElementType x);
ElementType popdata(mystack ms);
void hanoi(int pan);

void hanoi(int pan)
{
    ElementType tmp, toPush;
    mystack stk = create(STACKSIZE);
    tmp.a = 'a'; tmp.b = 'b'; tmp.c = 'c'; tmp.n = pan;
    pushdata(stk, tmp);
    while (!isEmpty(stk))
    {
        tmp = popdata(stk);
        if (tmp.n == 1)
        {
            printf("%c -> %c\n", tmp.a, tmp.c);
        }
        else
        {
            toPush.n = tmp.n - 1; toPush.a = tmp.b; toPush.b = tmp.a, toPush.c = tmp.c;
            pushdata(stk, toPush);
            toPush.n = 1; toPush.a = tmp.a; toPush.b = tmp.b, toPush.c = tmp.c;
            pushdata(stk, toPush);
            toPush.n = tmp.n - 1; toPush.a = tmp.a; toPush.b = tmp.c, toPush.c = tmp.b;
            pushdata(stk, toPush);
        }
    }
}



bool isEmpty(mystack ms)
{
    return (ms->top == -1);
}

bool isFull(mystack ms)
{
    return (ms->top == ms->maxsize - 1);
}

bool pushdata(mystack ms, ElementType x)
{
    if (isFull(ms))
    {
        printf("Stack is full.\n");
        return false;
    }
    else
    {
        ms->data[++(ms->top)] = x;
        return true;
    }
}

ElementType popdata(mystack ms)
{
    if (isEmpty(ms))
    {
        printf("Stack is empty.\n");
        return ERROR;
    }
    else
    {
        return (ms->data[(ms->top)--]);
    }
}


int main()
{
    int n;
    scanf("%d", &n);
    hanoi(n);
}

递归算法:

#include<stdio.h>
void hanoi(int n, char a, char b, char c)
{
	if (n == 1)
		printf("%c->%c\n", a, c);
	else
	{
		hanoi(n - 1, a, c, b);
		printf("%c->%c\n", a, c);
		hanoi(n - 1, b, a, c);
	}
}
int main()
{
	int n;
	scanf("%d", &n);
     hanoi(n,'A','B','C');
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值