c语言递归实现汉诺塔

代码不是自己写的,copy数据结构书上的,看的懂,但是写不出来。

//代码很简洁,但却是经典

#include <stdio.h>

int count =0;

void move(char x,int n,char y)
{
    count++;
}

// 从x开始,y做辅助塔,移动到z上 
void hanoi(int n,char x, char y, char z)
{
    if(n == 1)
    {
        move(x,1,z);    
    }
    else
    {
        hanoi(n-1,x,z,y);
        
        move(x,n,z);
        
        hanoi(n-1,y,x,z);    
    }    
}

int main()
{
    char a;
    char b;
    char c;
    hanoi(3,a,b,c);
    printf("共移动:%d",count);
    return 0;    

猜你喜欢

转载自blog.csdn.net/qq_40240102/article/details/83385627