杭电1372--Knight Moves(BFS)

来源:互联网 发布:数据采集卡原理设计 编辑:程序博客网 时间:2024/05/22 12:56
Problem Description
A friend of you is doing research on the Traveling KnightProblem (TKP) where you are to find the shortest closed tour ofknight moves that visits each square of a given set of n squares ona chessboard exactly once. He thinks that the most difficult partof the problem is determining the smallest number of knight movesbetween two given squares and that, once you have accomplishedthis, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to writea program that solves the "difficult" part.

Your job is to write a program that takes two squares a and b asinput and then determines the number of knight moves on a shortestroute from a to b.
 

Input
The input file will contain one or more test cases. Each testcase consists of one line containing two squares separated by onespace. A square is a string consisting of a letter (a-h)representing the column and a digit (1-8) representing the row onthe chessboard.
 

Output
For each test case, print one line saying "To get from xx toyy takes n knight moves.".
 

Sample Input
e2 e4 a1 b2b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6
 

Sample Output
To get frome2 to e4 takes 2 knight moves. To get from a1 to b2 takes 4 knightmoves. To get from b2 to c3 takes 2 knight moves. To get from a1 toh8 takes 6 knight moves. To get from a1 to h7 takes 5 knight moves.To get from h8 to a1 takes 6 knight moves. To get from b1 to c3takes 1 knight moves. To get from f6 to f6 takes 0 knightmoves.

# include<stdio.h>

# include<string.h>

int mark[10][10],x1,y1,x2,y2;

int dir[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};

char s1[5],s2[5];

struct node{

      int x,y,step;

};

struct node queue[100];

int bfs()

{

      int i,head,tail;

      struct node cur,next;

      head=0;

      tail=1;

      while(head<tail)

      {

             cur=queue[head++];

             for(i=0;i<8;i++)

             {

                    next.x=cur.x+dir[i][0];

                    next.y=cur.y+dir[i][1];

                    next.step=cur.step+1;

                    if(next.x>=1&&next.x<=8&&next.y>=1&&next.y<=8&&mark[next.x][next.y]==0)

                    {

                           if(next.x==x2&&next.y==y2)

                                  return next.step;

                           else

                           {

                                  mark[next.x][next.y]=1;

                                  queue[tail++]=next;

                           }

                    }

             }

      }

      return -1;

}

int main()

{

      int t;

      while(scanf("%s%s",s1,s2)!=EOF)

      {

             x1=s1[1]-'0';

             y1=s1[0]-'a'+1;

             x2=s2[1]-'0';

             y2=s2[0]-'a'+1;

             queue[0].x=x1;

             queue[0].y=y1;

             queue[0].step=0;

             memset(mark,0,sizeof(mark));

             if(x1==x2&&y1==y2)

             {

                    t=0;

                    printf("To get from %s to %s takes %d knightmoves.\n",s1,s2,t);

                    continue;

             }

             mark[x1][y1]=1;

             t=bfs();

             printf("To get from %s to %s takes %d knightmoves.\n",s1,s2,t);

      }

      return 0;

}

0 0