Hdu1372 knight moves

Source: Internet
Author: User
Knight moves

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 4813 accepted submission (s): 2957

Problem descriptiona friend of you is doing research on the traveling knight problem (tkp) where you are 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 that
The most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour wocould be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

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

Inputthe input file will contain in one or more test cases. each test case consists of one line containing two 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.

Outputfor each test case, 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.
 

Sourceuniversity of Ulm Local contest 1996
 

Recommendeddy
Solution: different search methods give priority to the breadth of search, as long as you know the direction, you can directly search, the other is handed over to the computer, you only need to lose the conditions and other results.
Because it is in the 'day' font, there are eight directions if the current is in the middle ., If it is at '0', there are eight directions ('1 ).

# Include <cstdio> # include <cstring> # include <queue> using namespace STD; int map [9] [9]; int n = 8; int dir [8] [2] = {1, 1,-2,-1,-1,-2, 2, 1,-2,-2,-2,-2, -1}; // eight directions: Char A [2] [2]; int Sx, Sy, ex, ey; struct node {int X; int y; int step ;}; bool inmap (int x, int y) {If (x> = 0 & x <n & Y> = 0 & Y <n) return true; return false;} int BFS () {node S, E; queue <node> q; S. X = SX; S. y = sy; S. step = 0; Map [SX] [sy] = 1; q. push (s); If (SX = ex & Sy = Ey) return 0; while (! Q. empty () {S = Q. front (); q. pop (); For (INT I = 0; I <8; I ++) // go directly to {e. X = S. X + dir [I] [0]; E. y = S. Y + dir [I] [1]; E. step = S. step + 1; if (inmap (E. x, E. Y )&&! Map [E. x] [E. y]) {If (E. X = ex & E. y = ey) Return e. step; Map [E. x] [E. y] = 1; q. push (e) ;}}return-1 ;}int main () {While (scanf ("% S % s", a [0], a [1])! = EOF) {memset (MAP, 0, sizeof (MAP); SX = A [0] [0]-'A'; // storage format conversion, the array uses Sy = A [0] [1]-'1'; Ex = A [1] [0]-'A '; ey = A [1] [1]-'1'; printf ("to get from % C to % C takes % d knight moves. \ n ", a [0] [0], a [0] [1], a [1] [0], a [1] [1], BFS ();} return 0 ;}

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.