Knight's Movement
Title Link: Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83498#problem/E
Topic:
Description
a Friend of You is doing-on The  Traveling Knight problem (TKP) where you is to find the shortest closed to ur of knight moves that visits each square of a given set Of  n squares on a chessboard exactly once. He thinks the most difficult part of the problem is determining the smallest number of knight moves between the given Squares and that, once you has accomplished this, finding the tour would is easy.
Your job is to write a program, takes, squares a and b as input and then determin Es the number of knight moves on a shortest route from a to b.
Input Specification
The input file is contain one or more test cases. Each test case consists the one line containing the squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing The row on the chessboard.
Output Specification
for each test case, print one line saying ' to get From
Sample Input
E2 E4A1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 f6
Sample Output
To get from E2 to E4 takes 2 Knight moves. To get from A1 to B2 takes 4 knight moves. To get from B2 to C3 takes 2 knight moves. To get from A1 to H8 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 C3 takes 1 Knight moves. To get from F6 to F6 takes 0 knight moves.
Test instructions
Enter the standard 8*8 two squares on the chess board (the column is represented by a a~h, the line is represented by 1~8),
The minimum number of steps required for a horse to jump from the starting point to the end.
Analysis:
The horse can walk in eight directions at a time, search directly with BFS;
Be aware, however, that when a horse is on the border of a chessboard, there is no direction (not crossing).
There is no way to repeat (mark the place where you walked).
1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <queue>5 using namespacestd;6 intc[9][9];7 intdir[8][2] = {{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};8typedefstruct9 {Ten intX,y,count; One }node; A node start,finish; - intMain () - { the CharRow,end; - intcol,ed; - intmin; - while(SCANF ("%c", &row)! =EOF) + { -scanf"%d",&col); + GetChar (); Ascanf"%c%d",&end,&ed); at GetChar (); -Start.x = row-'a'+1; -Start.y =Col; -finish.x = end-'a'+1; -FINISH.Y =Ed; - if(start.x==finish.x&&start.y==finish.y) inMin =0; - Else to { +Memset (c,0,sizeof(c)); - node pre,cur; theStart.count =0; *Queue<node>Q; $ Q.push (start);Panax NotoginsengC[START.X][START.Y] =1; - while(!q.empty ()) the { +Pre =Q.front (); A Q.pop (); the if(pre.x = = Finish.x&&pre.y = =finish.y) +min=Pre.count; - for(inti =0; I <8; i++) $ { $cur.x = pre.x + dir[i][0]; -CUR.Y = Pre.y + dir[i][1]; - if(cur.x<1|| Cur.x>8|| cur.y<1|| Cur.y>8)Continue; the if(c[cur.x][cur.y]==1)Continue; -C[CUR.X][CUR.Y] =1;WuyiCur.count = Pre.count +1; the Q.push (cur); - } Wu } - } Aboutprintf"To get from%c%d to%c%d takes%d Knight moves.\n", row,col,end,ed,min); $ } - return 0; -}
The movement of the BFS Knight