2017.11.26 计算机算法之分治与递归——汉诺塔

1、我的递归算法(纯粹的递归)

#include <stdio.h>//当盘子数n等于15时,移动次数已经达到32767,运行时间已经达到15.540s
long long count;
void hanoi(int n,char a,char b,char c)//借助C将A上的盘子全部移动到B
{
    if(n==0)
        return;
    hanoi(n-1,a,c,b);
    printf("%c --> %c
",a,b);
    count++;
    hanoi(n-1,c,b,a);
}
int main()
{
    int n;
    while(true)
    {
        count=0;
        printf("please putin the number of disk: 
");
        scanf("%d",&n);
        printf("the step of move the %d disks show below:
",n);
        hanoi(n,'A','B','C');
        printf("the times ot move is: %I64d
",count);
    }
    return 0;
}

2、书上的一个较慢的纯递归算法,运算15个盘子时,需要41.030s…….

#include<iostream>
using namespace std;
long long count;
int main()
{
 void hanoi(int n,char one,char two,char three);
 int m;
 count=0;
 cout<<"input the number of disks:";
 cin>>m;
 cout<<"The steps of moving"<<m<<"disks:"<<endl;
    hanoi(m,'A','B','C');
cout<<count;
 return 0;
}
void hanoi(int n,char one,char two,char three)
{
 void move(char x,char y);
 if(n==1)
  move(one,three);
 else{hanoi(n-1,one,three,two);
 move(one,three);
 hanoi(n-1,two,one,three);
 }
}
void move(char x,char y)
{
 cout<<x<<"-->"<<y<<endl;
 count++;
}

3、书上的非递归算法(其实就是仿《数学营养菜》(谈祥柏 著)中提供的一种方法),计算15个盘子时,运行时间为7.390s

#include <stdio.h>
#define N 1000
long long count;
char ta[3]={'C','A','B'};
char ta2[3]={'A','B','C'};
bool isodd(int n)
{
    if(n%2)
        return true;
    return false;
}
void hanoi(int n)
{
    int i;
    int top[3]={0,0,0};
    int tower[N][3];
    int bb,x,y,min=0;
    bool b;
    for(i=0;i<=n;++i)
    {
        tower[i][0]=n-i+1; tower[i][1]=n+1; tower[i][2]=n+1;
    }
    top[0]=n;
    b=isodd(n);
    bb=1;
    while(top[1]<n)
    {
        if(bb)
        {
            x=min;
            if(b)
                y=(x+1)%3;
            else
                y=(x+2)%3;
            min=y;
            bb=0;
        }
        else
        {
            x=(min+1)%3;
            y=(min+2)%3;
            bb=1;
            if(tower[top[x]][x]>tower[top[y]][y])
            {
                int tmp; tmp=x; x=y; y=tmp;
            }
        }
        //printf("OK1
");
        //printf("%c -%d-> %c
",ta[(x+1)%3],tower[top[x]][x],ta[(y+1)%3]);
        printf("%c -%d-> %c
",ta2[x],tower[top[x]][x],ta2[y]);
        //printf("%d %d %d %d
",x,y,top[x],top[y]);
        tower[top[y]+1][y]=tower[top[x]][x];
        //printf("OK3
");
        top[x]--; top[y]++;
        count++;
    }
}
int main()
{
    int n;
    while(true)
    {
        count=0;
        printf("please putin the number of disks:
");
        scanf("%d",&n);
        printf("the step of move the %d disks show below:
",n);
        hanoi(n);
        printf("the times ot move is: %I64d
",count);
    }
    return 0;
}

4、网上一个优秀的非递归算法(用栈模仿递归),计算15个盘子时需要6.880s
我在这里根据《数学营养菜》(谈祥柏 著)提供的一种方法,编了一个程序来实现。

#include <iostream>
using namespace std;
const int MAX = 64; //圆盘的个数最多为64
struct st{  //用来表示每根柱子的信息
      int s[MAX]; //柱子上的圆盘存储情况
      int top; //栈顶,用来最上面的圆盘
      char name; //柱子的名字,可以是A,B,C中的一个
      int Top()//取栈顶元素
      {
            return s[top];
      }
      int Pop()//出栈
      {
            return s[top--];
      }
      void Push(int x)//入栈
      {
            s[++top] = x;
      }
} ;
long Pow(int x, int y); //计算x^y
void Creat(st ta[], int n); //给结构数组设置初值
void Hannuota(st ta[], long max); //移动汉诺塔的主要函数
int main(void)
{
      int n;
      cin >> n; //输入圆盘的个数
     
      st ta[3]; //三根柱子的信息用结构数组存储
      Creat(ta, n); //给结构数组设置初值
 
      long max = Pow(2, n) - 1;//动的次数应等于2^n - 1
      Hannuota(ta, max);//移动汉诺塔的主要函数
 
      system("pause");
      return 0;
}
void Creat(st ta[], int n)
{
      ta[0].name = 'A';
      ta[0].top = n-1;
      for (int i=0; i<n; i++) //把所有的圆盘按从大到小的顺序放在柱子A上
            ta[0].s[i] = n - i;
      ta[1].top = ta[2].top = 0;//柱子B,C上开始没有没有圆盘
      for (int i=0; i<n; i++)
            ta[1].s[i] = ta[2].s[i] = 0;
      if (n%2 == 0) //若n为偶数,按顺时针方向依次摆放A B C
      {
            ta[1].name = 'B';
            ta[2].name = 'C';
      }
      else  //若n为奇数,按顺时针方向依次摆放 A C B
      {
            ta[1].name = 'C';
            ta[2].name = 'B';
      }
}
long Pow(int x, int y)
{
      long sum = 1;
      for (int i=0; i<y; i++)
            sum *= x;
 
      return sum;
}
void Hannuota(st ta[], long max)
{
      int k = 0; //累计移动的次数
      int i = 0;
      int ch;
      while (k < max)
      {
            //按顺时针方向把圆盘1从现在的柱子移动到下一根柱子
            ch = ta[i%3].Pop();
            ta[(i+1)%3].Push(ch);
            cout << ++k << ": " << "Move disk " << ch << " from " << ta[i%3].name << " to " << ta[(i+1)%3].name << endl;
            i++;
            //把另外两根柱子上可以移动的圆盘移动到新的柱子上
            if (k < max)
            {     //把非空柱子上的圆盘移动到空柱子上,当两根柱子都为空时,移动较小的圆盘
                  if (ta[(i+1)%3].Top() == 0 || ta[(i-1)%3].Top() > 0 && ta[(i+1)%3].Top() > ta[(i-1)%3].Top())
                  {
                        ch =  ta[(i-1)%3].Pop();
                        ta[(i+1)%3].Push(ch);
                        cout << ++k << ": " << "Move disk " << ch << " from " << ta[(i-1)%3].name << " to " << ta[(i+1)%3].name << endl;
                  }
                  else
                  {
                        ch =  ta[(i+1)%3].Pop();
                        ta[(i-1)%3].Push(ch);
                        cout << ++k << ": " << "Move disk " << ch << " from " << ta[(i+1)%3].name << " to " << ta[(i-1)%3].name << endl;
                  }
            }
      }
}
原文地址:https://www.cnblogs.com/qichunlin/p/7900819.html