VC++實(shí)現(xiàn)模擬漢諾塔效果
先上效果圖
再附上源代碼:
漢諾塔:
#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(); // 繪出移動(dòng)一次后漢諾塔的狀態(tài)
}
// Print And Move
void printandmove(int n, char x, char y)
{
printf("\n %d 號(hào)盤(pán)從 %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為漢諾塔盤(pán)子數(shù),如要改變,只需更改初始值即可。
char one = 'A', two = 'B', three = 'C';
printf("請(qǐng)輸入盤(pán)子個(gè)數(shù)[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òng)過(guò)程[%d個(gè)盤(pán)]\n\n", n);
drawtower(); // 繪出漢諾塔初始狀態(tài)
hanoi(n, one, two, three);
printf("\n 模擬結(jié)束,共移動(dòng)%ld次\n", (long)pow(2, n) - 1);
}
else
{
printf("數(shù)據(jù)錯(cuò)誤!\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(); // 繪出移動(dòng)一次后漢諾塔的狀態(tài)
}
// Print And Move
void printandmove(int n, char x, char y)
{
printf("\n %d 號(hào)盤(pán)從 %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為漢諾塔盤(pán)子數(shù),如要改變,只需更改初始值即可。
char one = 'A', two = 'B', three = 'C';
printf("請(qǐng)輸入盤(pán)子個(gè)數(shù)[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òng)過(guò)程[%d個(gè)盤(pán)]\n\n", n);
drawtower(); // 繪出漢諾塔初始狀態(tài)
hanoi(n, one, two, three);
printf("\n 模擬結(jié)束,共移動(dòng)%ld次\n", (long)pow(2, n) - 1);
}
else
{
printf("數(shù)據(jù)錯(cuò)誤!\n");
}
}
以上所述就是關(guān)于VC++實(shí)現(xiàn)漢諾塔效果的全部代碼了,希望對(duì)大家理解漢諾塔算法能夠有所幫助。
相關(guān)文章
C語(yǔ)言字符函數(shù)、內(nèi)存函數(shù)功能及實(shí)現(xiàn)代碼
這篇文章主要介紹了C語(yǔ)言字符函數(shù)、內(nèi)存函數(shù) 功能及實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02解析c++中參數(shù)對(duì)象與局部對(duì)象的析構(gòu)順序的詳解
本篇文章是對(duì)c++中參數(shù)對(duì)象與局部對(duì)象的析構(gòu)順序進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C++實(shí)現(xiàn)廣度優(yōu)先搜索實(shí)例
這篇文章主要介紹了C++實(shí)現(xiàn)廣度優(yōu)先搜索,對(duì)于C++程序員來(lái)說(shuō)非常有借鑒價(jià)值,需要的朋友可以參考下2014-08-08淺析VSCode tasks.json中的各種替換變量的意思 ${workspaceFolder} ${file} ${
這篇文章主要介紹了關(guān)于VSCode tasks.json中的各種替換變量的意思 ${workspaceFolder} ${file} ${fileBasename} ${fileDirname}等,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Matlab實(shí)現(xiàn)鼠標(biāo)光標(biāo)變成愛(ài)心和瞄準(zhǔn)鏡形狀
這篇文章主要為大家詳細(xì)介紹了如何利用Matlab實(shí)現(xiàn)將鼠標(biāo)光標(biāo)變成愛(ài)心和瞄準(zhǔn)鏡等形狀,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-08-08