HDU 1372.Knight Moves

Source: Internet
Author: User

Knight Moves Time limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i6 4u Submit Status Practice HDU 1372

Description

A Friend of you doing in the the Traveling Knight problem (TKP) where you were to find the shortest closed tour 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.
Of course you know the It is vice versa. Him to write a program that solves the "difficult" part.

Your job is to write a program, takes, squares A and B as input and then determines the number of knight moves on a Shortest route from A to B.

Input

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 CH Essboard.

Output

For each test case, the print one line saying "to get from XX to YY takes N Knight moves."

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.
In chess, the way the Knight (horse) walks is: go straight One, then one step further (that is, the combination of 1,2,-1,-2) because there are characters in the read data, be careful to avoid incorrect read-in carriage returns (\ n)The rest is the standard BFS template.
1 /*2 By:ohyee3 Github:ohyee4 Email:[email protected]5 Blog:http://www.cnblogs.com/ohyee/6 7 かしこいかわいい? 8 エリーチカ! 9 to write out the хорошо code OH ~Ten */ One  A#include <cstdio> -#include <algorithm> -#include <cstring> the#include <cmath> -#include <string> -#include <iostream> -#include <vector> +#include <list> -#include <queue> +#include <stack> A using namespacestd; at  - //DEBUG MODE - #defineDebug 0 -  - //Loops - #defineREP (n) for (int o=0;o<n;o++) in  - Const intMAXN =Ten; to intK[MAXN]; +  - Const intdelta[8] = {-1,-1,1,1,-2,2,2,-2}; the  * intBFS (intS1,intS2,intV1,intv2) { $     if(S1 = = V1 && S2 = =v2)Panax Notoginseng         return 0; -  thequeue<pair<int,int> >Q; +     BOOLVISITED[MAXN][MAXN]; AMemset (visited,false,sizeof(visited)); the     intDIS[MAXN][MAXN]; +memset (DIS,0,sizeof(DIS)); -  $Q.push (pair<int,int>(S1,S2)); $VISITED[S1][S2] =true; -  -      while(!Q.empty ()) { the         intTh1 =Q.front (). First; -         intTh2 =Q.front (). Second;Wuyi Q.pop (); the  -         //reach the end Wu         if(Th1 = = V1 && Th2 = =v2) -              Break; About  $         //Expand Nodes -         intnext1,next2; -          for(inti =0; I <8; i++) { -Next1 = Th1 +Delta[i]; ANEXT2 = Th2 + delta[7-i]; +             if(Next1 >8|| Next1 <1|| Next2 >8|| Next2 <1) the                 Continue; -             if(!Visited[next1][next2]) { $Q.push (pair<int,int>(NEXT1,NEXT2)); theVISITED[NEXT1][NEXT2] =true; theDIS[NEXT1][NEXT2] = Dis[th1][th2] +1; the             } the         } -     } in  the     if(Dis[v1][v2]) the         returnDis[v1][v2]; About     Else the         return-1; the } the  + BOOLDo () { -     CharS1,v1; the     intS2,v2;Bayi     if(SCANF ("%c%d%c%d\n", &s1,&s2,&v1,&v2) = =EOF) the         return false; theprintf"To get from%c%d to%c%d takes%d Knight moves.\n", -S1,S2,V1,V2,BFS (S1-'a'+1, S2,v1-'a'+1, v2)); -     return true; the } the  the intMain () { the      while(Do ()); -     return 0; the}

HDU 1372.Knight Moves

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.