请高手帮忙用c++写一下汉诺塔程序,谢谢!

hezhenh 2003-08-24 08:47:30
小弟想用c++实现汉诺塔,请高手帮忙!
...全文
139 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lianyingxunfeng 2003-09-21
  • 打赏
  • 举报
回复
//3.42 汉诺塔问题
/*
#include <iostream.h>

void hanoi( int n, int pole1, int pole2, int pole3 );
//n是盘子的数目,pole1是开始的柱子,pole3是作为目的地的柱子

int main()
{
cout <<"移动顺序是:" <<endl;
hanoi( 5, 1, 2, 3 );
return 0;
}

void hanoi( int n, int pole1, int pole2, int pole3 )
{
if( n != 1 ) {
hanoi( n - 1, pole1, pole3, pole2 );
cout <<pole1 <<" --> " <<pole3 <<endl;
hanoi( n -1, pole2, pole1, pole3 );
}
else
cout <<pole1 <<" --> " <<pole3 <<endl;
} */


//3.43 汉诺塔问题( 非递归实现 )
//难......简直变态!
/*
#include <iostream.h>

const width = rings + 1;

int main()
{
int rings, last, next, x, z[500], s[3];

rings = 4; //环的个数

for( x = 1; x <= rings; x++ ) //put rings on first peg
z[x] = width - x;
for( x = 0; x <= 2 * width; x += width ) //set base for each peg
z[x] = 1000;

if( rings % 2 == 0 ) {
last = 1; s[2] = 0; s[1] = 1;
z[width+1] = z[rings];
}
else
{
last = 2; s[1] = 0; s[2] = 1;
z[2 * width + 1] = z[rings];
}

cout <<"From 1 to" <<last + 1;
s[0] = rings - 1;

while( s[0] + s[1] ) //while first and second pegs aren't empty
{
//next ring to move is smaller of rings on the two pegs not moved onto last
if( last == 0 ) next = z[width +s[1]] < z[2 * width + s[2]] ? 1 : 2;
if( last == 1 ) next = z[s[0]] < z[2 * width + s[2]] ? 0 : 2;
if( last == 2 ) next = z[s[0]] < z[width + s[1]] ? 0 : 1;

//top ring of 'to' peg must be larger and an even 'distance' away
if( ( z[next * width + s[next]] > z[last * width + s[last]] ) ||
( ( z[last * width + s[last]] - z[next * width + s[next]] ) % 2 == 0 ) )
last = 3 - next - last;

cout <<"from" <<next + 1 <<"to" <<last + 1 <<endl;

s[next] = s[next] - 1;
s[last] = s[last] + 1; //move from 'next' to 'last' peg
z[last * width + s[last]] = z[next * width + s[next] + 1];
}
return 0;
} */
RookieStar 2003-09-21
  • 打赏
  • 举报
回复
这个递归问题到处都是,关键是你理解递归的精髓,以后不要再这么懒了!
andyhan12 2003-09-21
  • 打赏
  • 举报
回复
#include "stdafx.h"怎么无法打开这个文件呢?
mayzi 2003-09-21
  • 打赏
  • 举报
回复
老谈的书上有发行400万册的还有员代码
Smartdoggie 2003-08-25
  • 打赏
  • 举报
回复
1.递归ing pass给下一级函数
2.等上面处理好后输出自己负责的步骤

就这么简单...
代码自己写,有好处
catmiw 2003-08-25
  • 打赏
  • 举报
回复
这题自己都不会,建议趁早改行算了。
mjerry 2003-08-25
  • 打赏
  • 举报
回复
To a0002:
对此何必如此辛苦?
mjerry 2003-08-25
  • 打赏
  • 举报
回复
输入盘子数,得出各步骤
#include <stdio.h>
void move(int n,int a,int b,int c)
{if(n==1)printf("%d->%d\n",a,c);
else
{move(n-1,a,c,b);
printf("%d->%d\n",a,c);
move(n-1,b,a,c);
}
}
main()
{int n;
scanf("%d",&n);
move(n,1,2,3);
printf("\n");
}
短小精悍,所以给分.(谁叫你不好好看书,凡有递归必有此篇)
a0002 2003-08-25
  • 打赏
  • 举报
回复

To mjerry(冷雪),对一个新手来说,看一看别人的程序是很有好处的。如果你从来没有看过别人写的程序,你怎么知道自己的程序是否做的很好呢?

a0002 2003-08-25
  • 打赏
  • 举报
回复

Step: 0

||O_____|| ||______|| ||______||
||OO____|| ||______|| ||______||
||OOO___|| ||______|| ||______||
||OOOO__|| ||______|| ||______||
||OOOOO_|| ||______|| ||______||
||OOOOOO|| ||______|| ||______||
======================================================



Step: 1

||______|| ||______|| ||______||
||OO____|| ||______|| ||______||
||OOO___|| ||______|| ||______||
||OOOO__|| ||______|| ||______||
||OOOOO_|| ||______|| ||______||
||OOOOOO|| ||O_____|| ||______||
======================================================



Step: 2

||______|| ||______|| ||______||
||______|| ||______|| ||______||
||OOO___|| ||______|| ||______||
||OOOO__|| ||______|| ||______||
||OOOOO_|| ||______|| ||______||
||OOOOOO|| ||O_____|| ||OO____||
======================================================



Step: 3

||______|| ||______|| ||______||
||______|| ||______|| ||______||
||OOO___|| ||______|| ||______||
||OOOO__|| ||______|| ||______||
||OOOOO_|| ||______|| ||O_____||
||OOOOOO|| ||______|| ||OO____||
======================================================



Step: 4

||______|| ||______|| ||______||
||______|| ||______|| ||______||
||______|| ||______|| ||______||
||OOOO__|| ||______|| ||______||
||OOOOO_|| ||______|| ||O_____||
||OOOOOO|| ||OOO___|| ||OO____||
======================================================
a0002 2003-08-25
  • 打赏
  • 举报
回复

给你一个,汉诺塔的字符演示,编译后运行:han.exe 6 1000就每秒钟一步自动演示全过程,仅供参考:

// Han.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int count;
int autoPlay;
int ifCls;

void MoveOne(int base1, int base2, int **a)
{
a[base2][a[base2][count]] = a[base1][a[base1][count]-1];
a[base1][a[base1][count]-1] = 0;
a[base1][count] = a[base1][count] - 1;
a[base2][count] = a[base2][count] + 1;
}

void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}

void OutputState( int **a, int steps )
{
int i, j;

if(ifCls == 0) system("cls");
printf("\n\nStep: %5d\n\n", steps);
for(i = count - 1; i >= 0; i--)
{
printf("\t||");
for(j = 0; j < count; j++)
{
if(j < a[0][i])
printf("O");
else
printf("_");
}
printf("||\t||");
for(j = 0; j < count; j++)
{
if(j < a[1][i])
printf("O");
else
printf("_");
}
printf("||\t||");
for(j = 0; j < count; j++)
{
if(j < a[2][i])
printf("O");
else
printf("_");
}
printf("||\n");
}
for(i = 0; i < 3*(count + 8) + 12; i++)
printf("=");
printf("\n\n");
if(autoPlay == 0)
getchar();
else
sleep( (clock_t)autoPlay * CLOCKS_PER_SEC/CLOCKS_PER_SEC );
}

int Han(int n, int b1, int b2, int b3, int **a)
{
static int steps = 0;

if(n == 1)
{
MoveOne(b1, b3, a);
steps++;
OutputState( a, steps );
return steps;
}
Han(n-1, b1, b3, b2, a);
Han(1, b1, b2, b3, a);
Han(n-1, b2, b1, b3, a);

return steps;
}

int main(int argc, char* argv[])
{
int **a;
int b1, b2, b3;
int i, j;

count = 5;
autoPlay = 0;
ifCls = 0;
if(argc >= 2) count = atoi( argv[1] );
if(argc >= 3) autoPlay = atoi( argv[2] );
if(argc == 4) ifCls = 1;

a = (int **)malloc(3 * sizeof(int *));
for(i=0; i<3; i++)
a[i] = (int *)malloc((count+1)*sizeof(int));

b1 = 0;
b2 = 1;
b3 = 2;

for(i = 0; i < 3; i++)
for(j = 0; j < count+1; j++)
a[i][j] = 0;

for(j = 0; j < count; j++)
a[b1][j] = count - j;
a[b1][count] = count;
a[b2][count] = 0;
a[b3][count] = 0;

//printf("Step: %5d\n", 0);
OutputState( a, 0 );
int steps = Han(count, b1, b2, b3, a);
printf("Total steps: %6d\n\n", steps);

return 0;
}

Schlemiel 2003-08-24
  • 打赏
  • 举报
回复
没记错的话,老谭的C语言教材上有这个程序的例子,不要这么懒好不好。
西江残月 2003-08-24
  • 打赏
  • 举报
回复
和C没区别吧!除了语法!C++也得递归做啊!

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧