Sicily 1936. Knight Moves

Source: Internet
Author: User

1936. Knight Moves Constraints

Time limit:1 secs, Memory limit:32 MB

Description

a Friend of your is doing in the traveling Knight problem (TKP) where you were to find the SHO Rtest closed tour of Knight moves this 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

There is multiple test cases. The first line contains an integer T, indicating the number of 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
8e2 e4 A1 B2 B2 C3 A1 H8 A1 H7 H8 A1 B1 C3 f6 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.
Very egg pain of a problem, incredibly not to use gets!

The original code: 0.01s

#include <queue> #include <iostream> #include <stdio.h> #include <map> #include <algorithm > #include <string> #include <string.h>using namespace std; #define MAX 8bool Map[max][max];bool vis[max][    Max];int All_ans[max * max + 5][max * max + 5];struct point {int II; int JJ;}; Point start, End;bool the_same_point (Point A, point B) {return A.II = = B.II && a.jj = B.JJ;}  void output (int ans) {printf ("to get from%c%d to%c%d takes%d Knight moves.\n", Start.jj + ' a ', START.II + 1, END.JJ    + ' A ', End.ii + 1, ans); ALL_ANS[START.II * + START.JJ][END.II * + end.jj] = ans;}    void BFs () {memset (Vis, 0, sizeof (VIS));    Queue<point> Q;    Q.push (start);    int size, step = 0;    Point temp, Temp_next;        while (!q.empty ()) {step++;        Size = Q.size ();            while (size--) {temp = Q.front ();                        Q.pop (); if (Temp.ii > 1 && temp.jj > 0 &&!VIS[TEMP.II-2][temp.jj-1]) {TEMP_NEXT.II = temp.ii-2;                TEMP_NEXT.JJ = temp.jj-1;                if (The_same_point (Temp_next, end)) {output (step);                } q.push (Temp_next);            VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;                } if (Temp.ii > 1 && temp.jj < 7 &&!VIS[TEMP.II-2][TEMP.JJ + 1]) {                TEMP_NEXT.II = temp.ii-2;                TEMP_NEXT.JJ = temp.jj + 1;                if (The_same_point (Temp_next, end)) {output (step);                } q.push (Temp_next);            VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;                } if (Temp.ii > 0 && temp.jj < 6 &&!VIS[TEMP.II-1][TEMP.JJ + 2]) {                TEMP_NEXT.II = temp.ii-1;                TEMP_NEXT.JJ = Temp.jj + 2; if (The_same_point (Temp_next, end)) {output (step);                } q.push (Temp_next);            VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;                } if (Temp.ii < 7 && TEMP.JJ < 6 &&!vis[temp.ii + 1][TEMP.JJ + 2]) {                TEMP_NEXT.II = Temp.ii + 1;                TEMP_NEXT.JJ = Temp.jj + 2;                if (The_same_point (Temp_next, end)) {output (step);                } q.push (Temp_next);            VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;                } if (Temp.ii < 6 && TEMP.JJ < 7 &&!VIS[TEMP.II + 2][TEMP.JJ + 1]) {                TEMP_NEXT.II = Temp.ii + 2;                TEMP_NEXT.JJ = temp.jj + 1;                if (The_same_point (Temp_next, end)) {output (step);                } q.push (Temp_next);            VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true; if (Temp.ii < 6 && temp.jj > 0 &&!VIS[TEMP.II + 2][temp.jj-1]) {TEMP_NEXT.II = Temp.ii + 2;                TEMP_NEXT.JJ = temp.jj-1;                if (The_same_point (Temp_next, end)) {output (step);                } q.push (Temp_next);            VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;                } if (Temp.ii < 7 && temp.jj > 1 &&!vis[temp.ii + 1][temp.jj-2]) {                TEMP_NEXT.II = Temp.ii + 1;                TEMP_NEXT.JJ = temp.jj-2;                if (The_same_point (Temp_next, end)) {output (step);                } q.push (Temp_next);            VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;                } if (Temp.ii > 0 && temp.jj > 1 &&!vis[temp.ii-1][temp.jj-2]) {                TEMP_NEXT.II = temp.ii-1;                TEMP_NEXT.JJ = temp.jj-2;             if (The_same_point (Temp_next, end)) {       Output (step);                } q.push (Temp_next);            VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;    }}}} int main () {int case_num;    scanf ("%d\n", &case_num);    Char temp[5];        while (case_num--) {scanf ("%s", temp);        START.II = temp[1]-' 0 '-1;        START.JJ = temp[0]-' a ';        scanf ("%s", temp);        END.II = temp[1]-' 0 '-1;        END.JJ = temp[0]-' a ';        if (The_same_point (start, end)) {output (0);        } else {BFS ();                 }} return 0;}


Sicily 1936. 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.