VC++實(shí)現(xiàn)模擬漢諾塔效果
更新時(shí)間:2015年03月06日 09:40:15 投稿:hebedich
本文給大家分享的是一則使用vc++實(shí)現(xiàn)模擬漢諾塔效果的代碼,代碼實(shí)現(xiàn)起來很簡(jiǎn)單,主要是漢諾塔算法的思路要正確,正在練習(xí)漢諾塔的小伙伴也可以來看看,希望大家能夠喜歡。
先上效果圖

再附上源代碼:
漢諾塔:
復(fù)制代碼 代碼如下:
#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)盤從 %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為漢諾塔盤子數(shù),如要改變,只需更改初始值即可。
char one = 'A', two = 'B', three = 'C';
printf("請(qǐng)輸入盤子個(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)過程[%d個(gè)盤]\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
復(fù)制代碼 代碼如下:
/* 漢諾塔模擬
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)盤從 %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為漢諾塔盤子數(shù),如要改變,只需更改初始值即可。
char one = 'A', two = 'B', three = 'C';
printf("請(qǐng)輸入盤子個(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)過程[%d個(gè)盤]\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)文章
QT+ffmpeg實(shí)現(xiàn)視頻解析的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用QT+ffmpeg實(shí)現(xiàn)視頻解析功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Qt有一定幫助,需要的可以參考一下2022-09-09
C語(yǔ)言實(shí)現(xiàn)魔方陣算法(幻方陣 奇魔方 單偶魔方實(shí)現(xiàn))
魔方陣是指由1,2,3……n2填充的,每一行、每一列、對(duì)角線之和均相等的方陣,階數(shù)n = 3,4,5…。魔方陣也稱為幻方陣,看下面的實(shí)現(xiàn)方法吧2013-11-11
C++詳細(xì)分析lambda表達(dá)式的本質(zhì)
Lambda表達(dá)式是現(xiàn)代C++在C ++ 11和更高版本中的一個(gè)新的語(yǔ)法糖 ,在C++11、C++14、C++17和C++20中Lambda表達(dá)的內(nèi)容還在不斷更新。 lambda表達(dá)式(也稱為lambda函數(shù))是在調(diào)用或作為函數(shù)參數(shù)傳遞的位置處定義匿名函數(shù)對(duì)象的便捷方法2022-06-06
利用C語(yǔ)言實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的飛機(jī)游戲
在前面彈跳小球?的基礎(chǔ)上實(shí)現(xiàn)一個(gè)簡(jiǎn)單的飛機(jī)游戲,主要包括飛機(jī)的顯示、控制移動(dòng)、顯示復(fù)雜圖案、發(fā)射激光、打靶練習(xí)等功能,感興趣的可以嘗試一下2022-10-10
C語(yǔ)言中調(diào)用Swift函數(shù)實(shí)例詳解
這篇文章主要介紹了C語(yǔ)言中調(diào)用Swift函數(shù)實(shí)例詳解的相關(guān)資料,實(shí)現(xiàn)該功能可以通過定義全局的指向Blocks的對(duì)象指針來實(shí)現(xiàn),需要的朋友可以參考下2017-07-07
VisualStudio?制作Dynamic?Link?Library動(dòng)態(tài)鏈接庫(kù)文件的詳細(xì)過程
這篇文章主要介紹了VisualStudio?制作Dynamic?Link?Library動(dòng)態(tài)鏈接庫(kù)文件的詳細(xì)過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08

