Sicily Knight Moves (BFS)

Source: Internet
Author: User

Knight Moves
Time Limit:1sec Memory limit:32mbdescription

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

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 Copy sample input to Clipboard
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.

The problem and the front of the maze of the very similar, is here to go "Day" word, there are 8 directions. And there are no squares to go.
It is the vector to achieve BFS.
Just, the last output of a string of a space pits a little down.
1#include <iostream>2#include <queue>3#include <vector>4#include <cstring>5 using namespacestd;6 7 structnode8 {9     intx;Ten     inty; One }; A  - //Knight Walking Day - intmove[8][2] = {{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}}; the intused[9][9]; - intStartx,starty,endx,endy; -  - BOOLSafeintXinty) + { -     if(X >0&& y >0&& x <=8&& y <=8&& Used[x][y] = =0) +     { A         return true; at     } -     return false; - } -  - intMain () - { in     intT; -      toCin>>T; +      -      while(t--) the     { *         Chars,e; $Vector <node> v[ -];Panax Notoginseng                  -Cin>>s>>StartX; theStarty = S-'a'+1; +Cin>>e>>EndX; AEndy = e-'a'+1; the          + node No; -no.x =StartX; $NO.Y =Starty; $         intK =0; -         intFlag =0; -          thev[0].push_back (no); -         //Initialize with memsetWuyimemset (Used,0,sizeof(used)); theUsed[startx][starty] =1; -          Wu         if(startx! = EndX | | Starty! =Endy) -         { About                        //Vector realizes BFs $              while(1) -             { -Vector <node>:: iterator it; -                  for(It=v[k].begin (); It!=v[k].end (); it++) A                 { +Node F = *it; the                      for(inti =0; I <8; i++) -                     { $ node tmp; thetmp.x = f.x + move[i][0]; theTMP.Y = F.y + move[i][1]; the                         if(tmp.x = = EndX && Tmp.y = =Endy) the                         { -Flag =1; in                              Break; the                         } the                         if(Safe (tmp.x,tmp.y)) About                         { theV[k +1].push_back (TMP); theUSED[TMP.X][TMP.Y] =1; the                         } +                     } -                     if(Flag = =1) the                     {Bayi                          Break; the                     } the                 } -k++; -                 if(Flag = =1) the                 { the                      Break; the                 }     the             } -cout<<"To get from"<<s<<startx<<" to"<<e<<endx<<"takes"<<k<<"Knight moves."<<Endl; the              the         } the         Else94         { thecout<<"To get from"<<s<<startx<<" to"<<e<<endx<<"takes"<<"0"<<"Knight moves."<<Endl; the         } the     }98     return 0; About}

Sicily Knight Moves (BFS)

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.