Advertisement
lily09290110

c117遞回解西洋棋

Feb 1st, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int record_step=1000;
  4. int search(int x0,int y0,int x1,int y1,int count)
  5. {
  6.     if(count<record_step)
  7.     {
  8.         if(x0==x1&&y0==y1)record_step=count;
  9.         else if(x0<8&&x0>=0&&y0>0&&y0<=8)
  10.         {
  11.             search(x0+1,y0+2,x1,y1,count+1);
  12.             search(x0+1,y0-2,x1,y1,count+1);
  13.             search(x0-1,y0+2,x1,y1,count+1);
  14.             search(x0-1,y0-2,x1,y1,count+1);
  15.             search(x0+2,y0+1,x1,y1,count+1);
  16.             search(x0-2,y0+1,x1,y1,count+1);
  17.             search(x0+2,y0-1,x1,y1,count+1);
  18.             search(x0-2,y0-1,x1,y1,count+1);
  19.         }
  20.     }
  21. }
  22. int main(i)
  23. {
  24.     char word;
  25.     int star[2],end[2];
  26.     while(scanf("%c",&word)!=EOF)
  27.     {
  28.         star[0]=(int)(word-'a');
  29.         scanf("%d %c%d",&star[1],&word,&end[1]);
  30.         while(getchar()!='\n') continue;
  31.         end[0]=(int)(word-'a');
  32.         search(star[0],star[1],end[0],end[1],0);
  33.         printf("To get from %c%d to %c%d takes %d knight moves.\n",star[0]+'a',star[1],end[0]+'a',end[1],record_step);
  34.         record_step=1000;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement