骑士周游

adfas 2007-04-16 07:01:25
问1.骑士周游的 C++ 源代码.
2.从哪些方格开始可完成一个完整的周游.
谢谢高手解答
...全文
547 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
不想低调 2007-04-17
  • 打赏
  • 举报
回复
骑士周游算法--用链栈实现
骑士周游算法分析--用双向链表实现
http://blog.sina.com.cn/u/4a58d41f0100056n
wangzhangyong411 2007-04-16
  • 打赏
  • 举报
回复
c c++不都意义的嘛

自己概括改写下嘛

顶了

linuxhaha 2007-04-16
  • 打赏
  • 举报
回复
可以看看 <Java算法,第二卷 图算法>
adfas 2007-04-16
  • 打赏
  • 举报
回复
这个是c的源代码,不是c++的啊.不过还是谢谢.
可不可以给个c++的代码.
jixingzhong 2007-04-16
  • 打赏
  • 举报
回复
【问题分析】
(1) 棋盘的表示方法
我们可以用一个8×8的二维数组A(I,J)来表示国际象棋的棋盘,在马还没有开始周游棋盘时,棋盘上所有的格都置为零。以后,马跳到哪个格,就将马跳跃的步数记录在相应的空格里。
(2) 马的跳跃方向的确定
在国际象棋的棋盘上,一匹马共有八个可能的跳跃方向。

我们设置一组坐标增量来描述这八个条约方向:
① (1,2) ② (2,1)
③ (2,-1) ④ (1,-2)
⑤ (-1,-2) ⑥ (-2,-1)
⑦ (-2,1) ⑧ (-1,2)

(3) 马的跳跃方向的表示
设I表示行,J表示列,行增量为DI(R),列增量为DJ(R),则马向某个方向试探性地跳跃一步之后的新坐标应该表示为:NI=I+DI(R),NJ=J+DJ(R)。
(4) 朝某个方向试探性地跳跃一步再看下一步(取下一步最小可走方向(处里边角问题)),
任何一点的坐标加上要试探方向的坐标增量之后,都要判断一下是否已经超出了棋盘的边界。即:当I < 0,或I > 8,或J < 0,或J > 8时,都表示已经超出了棋盘的边界,这时,应该放弃该方向,转向试探下一个方向,在不出界的情况下,如果A(NI,NJ)=0,则表示该方向的前方有通路,可以继续向前跳跃。
如果A(NI,NJ)>0,则表示该格已经走过了,不能再走。放弃该方向,并转向下一个方向进行试探。

#include"stdio.h"
#include"conio.h"
#define N 19 /*How many the horse最好不要超过19(超出屏幕)*/

int top=0;

struct stack
{ int x;
int y;
int step;
}ma[N*N+1]={{0,0,0}};

void push(int a[][N],int i,int j,int m)
{ ma[top].x=i;
ma[top].y=j;
ma[top].step=m;
a[i][j]=++top;
}

int pop(int a[][N])
{ int temp;
top--;
a[ma[top].x][ma[top].y]=0;
ma[top].x=0;
ma[top].y=0;
temp=ma[top].step+1;
ma[top].step=0;
return temp;
}

int jump(int i,int j,int a[][8])
{ int col[]={2,1,-1,-2,-2,-1,1,2};
int row[]={-1,-2,-2,-1,1,2,2,1};
int t,ti=i,tj=j,count=0;
for(t=0;t<8;t++){
ti+=row[t];tj+=col[t];
if(judge(ti,tj,a))
count++; /*How many ways for the horse can jump for second time.*/
ti-=row[t];tj-=col[t];
}
return count;
}

int judge(int i,int j,int a[][N]) /*Judge the position can jump */
{ if(i>=0&&j>=0&&i<N&&j<N&&a[i][j]==0)
return 1;
return 0;
}

sort(int a[8],int b[8])
{ int i,min=a[0],t=0;
for(i=1;i<8;i++){
if(min>a[i]&&a[i]>-1&&a[i]<8){
min=a[i]; /*Find the Min-way the horse to jump*/
t=b[i];
}
}
return t;
}

void disp(int a[][N])
{ int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++)
printf("%4d",a[i][j]);
printf("\n");
}
}

void horse(int x,int y)
{ int i=x,j=y,min,ti,tj,t,temp=0,flag=0,temp1=0;
int count[8],num[8]={0};
int col[]={2,1,-1,-2,-2,-1,1,2};
int row[]={-1,-2,-2,-1,1,2,2,1};
int a[N][N]={{0}};
for(x=0;x<8;x++)
count[x]=8;
push(a,i,j,0);
while(top<N*N)
{ ti=i;tj=j;temp1=0;flag=0;
for(x=0;x<8;x++)
count[x]=8;
for(t=temp;t<8;t++,temp1++){ /*How many ways for the horse can jump for first time*/
ti+=row[t];tj+=col[t];
if(judge(ti,tj,a)){
count[temp1]=jump(ti,tj,a);
num[temp1]=t;
flag=1;
}
ti-=row[t];tj-=col[t];
}
if(flag){
min=sort(count,num);
ti+=row[min];tj+=col[min];
push(a,ti,tj,min); /*In the stack*/
i=ti;j=tj;
temp=0;
}
else{
temp=pop(a); /*Return the stack*/
i=ma[top-1].x;
j=ma[top-1].y;
}
}
printf("\n\n");
disp(a);
}

main()
{ int x,y;
do{
printf("Please enter the X-position :");
scanf("%d",&x);
printf("Please enter the Y-position :");
scanf("%d",&y);
if(x>N||y>N||x<1||y<1)
printf("Error! Try it again,X(1~%d),Y(1~%d)\n",N,N);
}while(x>N||y>N||x<1||y<1);
horse(x-1,y-1);
getch();
return 0;
}

64,639

社区成员

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

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