递归问题。

gladius 2000-08-12 11:19:00
谁知道如何用递归的方法实现骑士周游的问题?
类似于迷宫的问题。
...全文
553 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyqryq 2000-12-22
  • 打赏
  • 举报
回复
这是个基本问题,找一本书吧!
tycjg 2000-12-19
  • 打赏
  • 举报
回复
去datastructure.yeah.net看看。
fleg 2000-12-04
  • 打赏
  • 举报
回复
这个问题太简单了。
friendkey 2000-10-27
  • 打赏
  • 举报
回复
program horse;
uses crt;
type
TDirection=record
x,y:integer;
end;
TPos=TDirection;

const
a=5;
b=4;
Directions:array[1..8]of TDirection=((x:1;y:2),(x:-1;y:2),
(x:1;y:-2),(x:-1;y:-2),
(x:2;y:1),(x:-2;y:1),
(x:2;y:-1),(x:-2;y:-1));

var ch:array[1..b,1..a]of byte;Pos:TPos;cnt:byte;

procedure init;
var i,j:byte;
begin
for i:=1 to b do
for j:=1 to a do
ch[i,j]:=0;
Pos.x:=1;
Pos.y:=1;
cnt:=0
end;

function CanGo(aPos:TPos):boolean;
begin
CanGo:=(ch[aPos.y,aPos.x]=0)and(aPos.x in[1..a])and(aPos.y in[1..b])
end;

procedure Jump(aPos:TPos;aDirection:TDirection;var NextPos:TPos);
begin
NextPos.x:=aPos.x+aDirection.x;
NextPos.y:=aPos.y+aDirection.y;
end;

procedure pr;
var i,j:byte;
begin
for i:=1 to a do
for j:=1 to b do
begin
gotoxy(i*3,j);
write(ch[j,i]:2);
end;
end;

procedure search(aPos:TPos;d:integer);
var i:byte;NextPos:TPos;
begin
ch[aPos.y,aPos.x]:=d;
pr;
if (d=a*b) then
begin
readkey;
inc(cnt)
end;

for i:=1 to 8 do
begin
Jump(aPos,Directions[i],NextPos);
if CanGo(NextPos)then
begin
search(NextPos,d+1);
ch[NextPos.y,NextPos.x]:=0;
end;
end;
end;

begin
clrscr;
init;
search(Pos,1);
writeln;
write(cnt);
end.
friendkey 2000-10-27
  • 打赏
  • 举报
回复
PASCAL编的,可以吗?

program horse;
uses crt;
type
TDirection=record
x,y:integer;
end;
TPos=TDirection;

const
a=5;
b=4;
Directions:array[1..8]of TDirection=((x:1;y:2),(x:-1;y:2),
(x:1;y:-2),(x:-1;y:-2),
(x:2;y:1),(x:-2;y:1),
(x:2;y:-1),(x:-2;y:-1));

var ch:array[1..b,1..a]of byte;Pos:TPos;cnt:byte;

procedure init;
var i,j:byte;
begin
for i:=1 to b do
for j:=1 to a do
ch[i,j]:=0;
Pos.x:=1;
Pos.y:=1;
cnt:=0
end;

function CanGo(aPos:TPos):boolean;
begin
CanGo:=(ch[aPos.y,aPos.x]=0)and(aPos.x in[1..a])and(aPos.y in[1..b])
end;

procedure Jump(aPos:TPos;aDirection:TDirection;var NextPos:TPos);
begin
NextPos.x:=aPos.x+aDirection.x;
NextPos.y:=aPos.y+aDirection.y;
end;

procedure pr;
var i,j:byte;
begin
for i:=1 to a do
for j:=1 to b do
begin
gotoxy(i*3,j);
write(ch[j,i]:2);
end;
end;

procedure search(aPos:TPos;d:integer);
var i:byte;NextPos:TPos;
begin
ch[aPos.y,aPos.x]:=d;
pr;
if (d=a*b) then
begin
readkey;
inc(cnt)
end;

for i:=1 to 8 do
begin
Jump(aPos,Directions[i],NextPos);
if CanGo(NextPos)then
begin
search(NextPos,d+1);
ch[NextPos.y,NextPos.x]:=0;
end;
end;
end;

begin
clrscr;
init;
search(Pos,1);
writeln;
write(cnt);
end.
wangqiqi 2000-10-23
  • 打赏
  • 举报
回复
有优化一点的算法吗?我用这种算法对9*10的标准象棋盘花了15分钟都算不出一个方案出来。
sunruijia 2000-10-03
  • 打赏
  • 举报
回复
我们的答复满足您的要求了吗,如果满足了亲请按游戏规则给分吧。
whitehare 2000-09-28
  • 打赏
  • 举报
回复
哈哈哈哈哈?
……
^_^
这个算法很出名啊?

高级程序员(新) 教材上 P340 例8.11
写得不要太详细噢。




自已去找吧?
要我抄一大段代码,宁可不要这分数。
Sorry? Money 有限?^_^


whitehare 2000-09-28
  • 打赏
  • 举报
回复
哈哈哈哈哈?
水平考试书上看到过了自己去找吧!
gladius 2000-08-19
  • 打赏
  • 举报
回复
谢谢sunruijia兄!
下面是我一位好友帮忙写的递归算法,帖上来希望能对大家有所帮助。
未经他同意,还请他原谅!
//programming by titita
#include <stdio.h>
#include <conio.h>

#define MAX_XY 6
#define true 1
#define false 0
static int anDirections[][2]=
{ {2,1},{1,2},
{-1,-2},{-2,-1},
{1,-2},{-2,1},
{-1,2},{2,-1} };

typedef int status;
typedef struct
{
int nStep; // Which step this square is
}TSquare;

TSquare Board[MAX_XY][MAX_XY];

void RecurseFind( int startX, int startY );
void RecurseFindExecute( int x, int y, int step, status &bOK );

void main( int argc, char * argv[] )
{
int x,y;
printf("Input X:");
scanf("%d",&x);
printf("Input Y:");
scanf("%d",&y);

RecurseFind( x, y );
getch();
}

// 递归
void RecurseFind( int startX, int startY )
{
status& bOK = false;
int x,y;
for(x=0;x<MAX_XY;x++)
for(y=0;y<MAX_XY;y++)
Board[x][y].nStep=0;
Board[startX][startY].nStep=1;
RecurseFindExecute( startX,startY,2,bOK);
for(x=0;x<MAX_XY;x++)
{
for(y=0;y<MAX_XY;y++)
printf("%4d",Board[x][y].nStep);
printf("\n");
}
}

void RecurseFindExecute( int x, int y, int step, status &bOK )
{
if(step>MAX_XY*MAX_XY)
{
bOK = true;
return;
}

int newX, newY;
for(int i=0;i<8;i++)
{
newX = x + anDirections[i][0];
newY = y + anDirections[i][1];
if((newX>=0)&&(newX<MAX_XY)&&(newY>=0)&&(newY<MAX_XY))
if(!Board[newX][newY].nStep)
{
Board[newX][newY].nStep = step;
Board[newX][newY].nDirection=i;
RecurseFindExecute( newX, newY, step+1, bOK );
if(bOK) return;
else Board[newX][newY].nStep = 0;
}

}

}

sunruijia 2000-08-18
  • 打赏
  • 举报
回复
我以5*4的棋盘为例写了以下程序,希望对你有帮助。
#include"stdio.h"
int k[8]={2,-2,1,-1,2,-2,1,-1};
int m[8]={1,1,2,2,-1,-1,-2,-2};/*向8个方向跳*/
int a[100],b[100],chess[5][4];/*a[100],b[100]用来记录每次跳到的X,Y,chess[5][4]代表棋盘*/
main()
{
int dep=1,i=0,j=0,t=0,q=1,g,s,n[100];/*dep代表跳的步数,n[100]用来记录前一次跳的方向*/
for (g=0;g<=4;g++)
for(s=0;s<=3;s++)
chess[g][s]=0;
chess[0][0]=1;/*从(0,0)处跳*/
while(1)
{
while(t<=7)/*共0~7个方向,轮流试*/
{
a[q]=i;
b[q]=j;
i=i+m[t];
j=j+k[t];
if(j>=0 &&j<=3 &&i>=0&&i<=4&&chess[i][j]==0)/*判断所跳之处是否合法*/
{q++;n[q]=t;t=0;dep++;chess[i][j]
=dep;
if(dep==20) goto lable;/*一旦所跳之处合法,且全局走满则可打印结果了*/
}
else
{t++; i=a[q];j=b[q];}/*一种方向不行,换个方向再试*/
}
chess[i][j]=0;t=n[q]+1;i=a[--q];j=b[q];dep--;}/*8种方向都试完仍不行,则回溯*/
lable: for(g=0;g<=4;g++)
{
for(s=0;s<=3;s++)
printf("%d ",chess[g][s]);
printf("\n");
}
}
程序匆匆写成,能够运行并结果正确,但写的不好。有不当之处恳请指正。另外程序没有用递归,而是用堆栈消除了递归,我认为不用递归更好,可防止溢出,走迷宫等问题就肯定不能用递归了。
gladius 2000-08-14
  • 打赏
  • 举报
回复
能告诉我那里有介绍数据结构比较详细全面的站点吗?
谢谢!
gladius 2000-08-14
  • 打赏
  • 举报
回复
帮帮我,谢谢!
gladius 2000-08-13
  • 打赏
  • 举报
回复
就是在一个给定矩阵,比如说是6*6的,给定一个起始点:0,0 或者什么其他的3,5 。。。
然后按照象棋中马跳的方法(日字),从任意方向开始,遍历所有节点,就是从0~36,然后打印
路径。结果可以如下:
1 x x x x x
x x x x x x
x 2 x x x x
x x x 3 x x
x x x x x 4
x x x 5 x x
用栈的方法我已经实现了,就是想知道如何用递归的方法实现,谢谢大家帮助!
wwwunix 2000-08-13
  • 打赏
  • 举报
回复
能说详细点吗?
shines77 2000-08-13
  • 打赏
  • 举报
回复
你都没有说清除!??
gladius 2000-08-13
  • 打赏
  • 举报
回复
没有人知道吗?

33,006

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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