__________________________________统统给我进来____________________________________________________

TemplatesGuy 2005-04-21 08:45:39
偶现在研究递归中,求经典的递归代码。请帮忙找些资料;

P.S:
1.有四柱的汉诺塔问题的算法么?
2.给几个经典的N(8)皇后问题的算法代码。
3.最经典的三柱汉诺塔,偶也要。谢谢。



谢谢了。本帖乞求好心人。因为没有多少分。饿。
...全文
353 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
libbyliugang 2005-05-01
  • 打赏
  • 举报
回复
鸟大了,什么样的林子都有.............
abzhang2 2005-05-01
  • 打赏
  • 举报
回复
x y z
| | |
| | |
汉诺塔算法 (思想)
// hannota(n, x, y, z) 表示将塔x上1到n的n个圆盘移到z上,y作为中间塔
// move (x, n, z) 表示将编号为n的圆盘从x移动到z上
hannota( n, x, y, z)
{
if(n==1)
move(x,1,z);
else
{
hannota(n-1, x, z, y);
move(x, n, z);
hannota(n-1, y, x, z);
}
}
xuni1411 2005-04-28
  • 打赏
  • 举报
回复
网上都有~
以后发贴前到GOOGLE搜一下~
哗啦啦!!!!!!!!!!!!!
好多哦~~~~~~~~~~~~~~~
gumbour 2005-04-24
  • 打赏
  • 举报
回复
1.有四柱的汉诺塔问题的算法么?
用动态规划要比递归快很多。
2.给几个经典的N(8)皇后问题的算法代码。
所有的算法书上都有。
3.最经典的三柱汉诺塔,偶也要。谢谢。
C的教材上都有
jipuy 2005-04-24
  • 打赏
  • 举报
回复
汉诺塔问题:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#define MAXNUM 10
int hole[3][MAXNUM];

int plate_height[3]={0};

void drawplate(int x,int y,int wide)
{
setfillstyle(SOLID_FILL,wide/20);
bar(x-wide/2,y,x+wide/2,y+16-1);
}

void eraseplate(int x,int y,int wide)
{
setfillstyle(SOLID_FILL,BLACK);
bar(x-wide/2,y,x+wide/2,y+16-1);

}

void move(int x,int y)
{
int px,py,wide;

if(getch()==27)
{
closegraph();
exit(1);
}
wide=hole[x-1][plate_height[x-1]-1]*20;
px=100+200*(x-1);
py=480-plate_height[x-1]*16;
eraseplate(px,py,wide);
setfillstyle(SOLID_FILL,x-1+11);
bar(100+200*(x-1)-5,py,100+200*(x-1)+5,py+16-1);

hole[y-1][plate_height[y-1]]=hole[x-1][plate_height[x-1]-1];
plate_height[x-1]--;
plate_height[y-1]++;

px=100+200*(y-1);
py=480-plate_height[y-1]*16;
drawplate(px,py,wide);

delay(400);
}
void hanoi(int n,int one,int two,int three)
{
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void main()
{
int m,i;
int gdriver=DETECT,gmode;
printf("input the number of diskes:");
scanf("%d",&m);
if(m>10)
{
printf("Too many plates!\nYou would kill me?\n");
return;
}
for(i=m;i>=1;i--)
hole[0][m-i]=i;
plate_height[0]=m;
plate_height[1]=plate_height[2]=0;

printf("press any key to see the moving steps.");
getch();
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"");
for(i=0;i<3;i++)
{
setfillstyle(SOLID_FILL,11+i);
bar(100+200*i-5,480-16*12,100+200*i+5,480);
}
for(i=0;i<plate_height[0];i++)
{
int wide;
wide=hole[0][i]*20;
drawplate(100,480-(i+1)*16,wide);
}
hanoi(m,1,2,3);
getch();
closegraph();
}
//上面的是图形演示;


void move(int x,int y)
{
printf("move the top plate of %dth hole-->%dth hole\n",x,y);
}
void hanoi(int n,int one,int two,int three)
{
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
main()
{
int m;
printf("input the number of diskes:");
scanf("%d",&m);
printf("The step to moving %3d diskes:\n",m);
hanoi(m,1,2,3);
getch();
}
fireflyc 2005-04-23
  • 打赏
  • 举报
回复
脾气好大
chogo 2005-04-23
  • 打赏
  • 举报
回复
鸟大了,什么样的林子都有.............
TemplatesGuy 2005-04-22
  • 打赏
  • 举报
回复
为什么要BS我呢?
pcyy 2005-04-22
  • 打赏
  • 举报
回复
研究递归中
OldWaterKing 2005-04-22
  • 打赏
  • 举报
回复
ri
ayace 2005-04-22
  • 打赏
  • 举报
回复
进错了,

PS:太深奥,我算法白痴,还是先撤,顺便顶一下。
bigf1ingpig 2005-04-22
  • 打赏
  • 举报
回复
R_L 2005-04-22
  • 打赏
  • 举报
回复
BS
fortomorrow 2005-04-22
  • 打赏
  • 举报
回复
这在大学学习中都已经编过了!
给你找了找!
N皇后问题!不仅N为8,N可为任意。
绝对可以!
今天注册!第一次登陆!
#include "iostream.h"
#include "iomanip.h"
/*#include <valarray> //abs library
typedef valarray<int> INTVALARRAY;
using namespace std;*/
#include "math.h"
#define MAX 15



int X[MAX+1]; //全局变量,第k行的皇后放在X[k]列
int solution=0; //解的数目

int PLACE(int k)
//如果一个皇后能放在第k行和X[k]列,返回true,否则,返回false
{
int i;
i=1;
while(i<k)
{
if((X[i]==X[k])||((abs(X[i]-X[k]))==abs(i-k)))
//在同一列的皇后或在同一条斜对角线上
/*int temp1,temp2;
temp1=X[i]-X[k];
if(temp1<0)
temp1=-temp1;
temp2=i-k;
if(temp2<0)
temp2=-temp2;
if((X[i]==X[k])||(temp1==temp2))*/
return 0;
i++;
}
return 1;
}

void NQUEENS(int N)
{
int k,i;
X[1]=0;//k是当前行,X[k]是当前列
k=1;
while(k>0)
{
X[k]=X[k]+1;//移到下一列
while((X[k]<=N)&&(!PLACE(k)))//此处能放这个皇后吗?
X[k]=X[k]+1;
if(X[k]<=N)
if(k==N)
{
solution++;//解的个数增加1
cout<<"第"<<solution<<"个解为"<<endl;
for(i=1;i<=N;i++)
{
cout<<setw(3)<<X[i];//将解输出
}
cout<<endl;
}
else
{
k++;X[k]=0;//转向下一行
}
else k--;
}
}

void main()
{
int N;
void NQUEES(int N);
cout<<"你要解决的N皇后问题中的N值为?"<<endl;
cin>>N;
NQUEENS(N);
cout<<N<<"皇后共有"<<solution<<"个解! "<<endl;
}
xjp6688 2005-04-22
  • 打赏
  • 举报
回复
www.vcok.com
wizard13 2005-04-22
  • 打赏
  • 举报
回复
大学里,我写过四张牌算二十四点程序,挺经典的
mahatma_cn 2005-04-21
  • 打赏
  • 举报
回复
没有见过这么强劲的乞求的。我不是好心人!我是坏心人

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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