服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - VC++实现模拟汉诺塔效果

VC++实现模拟汉诺塔效果

2021-02-22 12:06C++教程网 C/C++

本文给大家分享的是一则使用vc++实现模拟汉诺塔效果的代码,代码实现起来很简单,主要是汉诺塔算法的思路要正确,正在练习汉诺塔的小伙伴也可以来看看,希望大家能够喜欢。

先上效果图

VC++实现模拟汉诺塔效果

再附上源代码:

汉诺塔:

 

复制代码 代码如下:

#include "stdio.h"
#include "math.h"
int arrA[15], arrB[15], arrC[15];   // 分别为A、B、C
int length;
int lenA, lenB, lenC;
char plate[32];
// Make
void makeplate(int n)
{
    int i;
    if (n == length + 1)
    {
        for (i = 0; i < 2 * length + 3; i++)
        {
            if (i == length + 1)
            {
                plate[i] = '|';
            }
            else
            {
                plate[i] = '_';
            }
        }
    }
    else
    {
        if (n == 0)
        {
            for (i = 0; i < 2 * length + 3; i++)
            {
                if (i == length + 1)
                {
                    plate[i] = '|';
                }
                else
                {
                    plate[i] = ' ';
                }
            }
        }
        else
        {
            for (i = 0; i < 2 * length + 3; i++)
            {
                if (i == length + 1)
                {
                    plate[i] = '|';
                }
                else
                {
                    if (i >= length + 1 - n && i <= length || i > length + 1
                        && i <= length + 1 + n)
                    {
                        plate[i] = '_';
                    }
                    else
                    {
                        plate[i] = ' ';
                    }
                }
            }
        }
    }
    plate[i] = '\0';
}
// Draw
void drawtower()
{
    int i;
    printf(" ");
    for (i = length; i >= 0; i--)
    {
        if (i <= lenA)
        {
            makeplate(arrA[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        if (i <= lenB)
        {
            makeplate(arrB[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        if (i <= lenC)
        {
            makeplate(arrC[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        printf("\n ");
    }
}
// Move
void moveplate(int n, char x, char y)
{
    int i, j;
    if (x == 'A')
    {
        lenA--;
    }
    else
    {
        if (x == 'B')
        {
            lenB--;
        }
        else
        {
            lenC--;
        }
    }
    if (y == 'A')
    {
        lenA++;
        arrA[lenA] = n;
    }
    else
    {
        if (y == 'B')
        {
            lenB++;
            arrB[lenB] = n;
        }
        else
        {
            lenC++;
            arrC[lenC] = n;
        }
    }
    drawtower();                // 绘出移动一次后汉诺塔的状态
}
// Print And Move
void printandmove(int n, char x, char y)
{
    printf("\n %d 号盘从 %c 柱移到 %c 柱\n\n", n, x, y);
    moveplate(n, x, y);
}
// Hanoi
void hanoi(int n, char one, char two, char three)
{
    if (n == 1)
    {
        printandmove(n, one, three);
    }
    else
    {
        hanoi(n - 1, one, three, two);
        printandmove(n, one, three);
        hanoi(n - 1, two, one, three);
    }
}
// Main
void main()
{
    int n, i;                   // n为汉诺塔盘子数,如要改变,只需更改初始值即可。
    char one = 'A', two = 'B', three = 'C';
    printf("请输入盘子个数[1—12]:");
    scanf("%d", &n);
    if (n >= 1 && n <= 12)
    {
        length = n;
        lenA = n;
        for (i = 0; i <= lenA; i++)
        {
            arrA[i] = n + 1 - i;
        }
        lenB = lenC = 0;
        arrB[0] = arrC[0] = n + 1;
        printf(" 汉诺塔模拟移动过程[%d个盘]\n\n", n);
        drawtower();            // 绘出汉诺塔初始状态
        hanoi(n, one, two, three);
        printf("\n 模拟结束,共移动%ld次\n", (long)pow(2, n) - 1);
    }
    else
    {
        printf("数据错误!\n");
    }
}

 

汉诺塔.c

 

复制代码 代码如下:

/* 汉诺塔模拟
   2013-5-13
*/
#include "stdio.h"
#include "math.h"
int arrA[15], arrB[15], arrC[15];   // 分别为A、B、C
int length;
int lenA, lenB, lenC;
char plate[32];
// Make
void makeplate(int n)
{
    int i;
    if (n == length + 1)
    {
        for (i = 0; i < 2 * length + 3; i++)
        {
            if (i == length + 1)
            {
                plate[i] = '|';
            }
            else
            {
                plate[i] = '_';
            }
        }
    }
    else
    {
        if (n == 0)
        {
            for (i = 0; i < 2 * length + 3; i++)
            {
                if (i == length + 1)
                {
                    plate[i] = '|';
                }
                else
                {
                    plate[i] = ' ';
                }
            }
        }
        else
        {
            for (i = 0; i < 2 * length + 3; i++)
            {
                if (i == length + 1)
                {
                    plate[i] = '|';
                }
                else
                {
                    if (i >= length + 1 - n && i <= length || i > length + 1
                        && i <= length + 1 + n)
                    {
                        plate[i] = '_';
                    }
                    else
                    {
                        plate[i] = ' ';
                    }
                }
            }
        }
    }
    plate[i] = '\0';
}
// Draw
void drawtower()
{
    int i;
    printf(" ");
    for (i = length; i >= 0; i--)
    {
        if (i <= lenA)
        {
            makeplate(arrA[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        if (i <= lenB)
        {
            makeplate(arrB[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        if (i <= lenC)
        {
            makeplate(arrC[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        printf("\n ");
    }
}
// Move
void moveplate(int n, char x, char y)
{
    int i, j;
    if (x == 'A')
    {
        lenA--;
    }
    else
    {
        if (x == 'B')
        {
            lenB--;
        }
        else
        {
            lenC--;
        }
    }
    if (y == 'A')
    {
        lenA++;
        arrA[lenA] = n;
    }
    else
    {
        if (y == 'B')
        {
            lenB++;
            arrB[lenB] = n;
        }
        else
        {
            lenC++;
            arrC[lenC] = n;
        }
    }
    drawtower();                // 绘出移动一次后汉诺塔的状态
}
// Print And Move
void printandmove(int n, char x, char y)
{
    printf("\n %d 号盘从 %c 柱移到 %c 柱\n\n", n, x, y);
    moveplate(n, x, y);
}
// Hanoi
void hanoi(int n, char one, char two, char three)
{
    if (n == 1)
    {
        printandmove(n, one, three);
    }
    else
    {
        hanoi(n - 1, one, three, two);
        printandmove(n, one, three);
        hanoi(n - 1, two, one, three);
    }
}
// Main
void main()
{
    int n, i;                   // n为汉诺塔盘子数,如要改变,只需更改初始值即可。
    char one = 'A', two = 'B', three = 'C';
    printf("请输入盘子个数[1—12]:");
    scanf("%d", &n);
    if (n >= 1 && n <= 12)
    {
        length = n;
        lenA = n;
        for (i = 0; i <= lenA; i++)
        {
            arrA[i] = n + 1 - i;
        }
        lenB = lenC = 0;
        arrB[0] = arrC[0] = n + 1;
        printf(" 汉诺塔模拟移动过程[%d个盘]\n\n", n);
        drawtower();            // 绘出汉诺塔初始状态
        hanoi(n, one, two, three);
        printf("\n 模拟结束,共移动%ld次\n", (long)pow(2, n) - 1);
    }
    else
    {
        printf("数据错误!\n");
    }
}

 

以上所述就是关于VC++实现汉诺塔效果的全部代码了,希望对大家理解汉诺塔算法能够有所帮助。

延伸 · 阅读

精彩推荐