骑士周游快速算法,来自网络

骑士周游世界之快速周游(小于1秒)**走一步看两步**  
责任编辑:育鹰   更新日期:2005-8-6  
 
作者:许夏     gordon772@yahoo.com.cn
 
曾经有网友发表跳马的C程序,但发现在棋盘较大时时间很长,今对其改进,本题使用栈,但你会发现不用退栈,只用N*N步走完,(其实不用栈,用数组)。
 
骑士周游世界
在一个8×8的方格棋盘中,按照国际象棋中马的行走规则从棋盘上的某一方格出发,开始在棋盘上周游,如果能不重复地走遍棋盘上的每一个方格,这样的一条周游路线在数学上被称为国际象棋盘上马的哈密尔顿链。请你设计一个程序,从键盘输入一个起始方格的坐标,由计算机自动寻找并打印出国际象棋盘上马的哈密尔顿链。
                
【问题分析】
(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;
}
 
 

转载于:https://www.cnblogs.com/qzchenwl/archive/2008/10/06/1304400.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值