C-Broad Search Foundation
crawling in process ... crawling failed time limit:1000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64u
Submit Status
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 would 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.
Code:
/*
Simple BFS to find the shortest path length
*/
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace Std;
const double eps=1e-8;
Const double Pi=acos (-1.0);
int m[10][10];//used to mark, avoid repetition, BFS common pruning
int ax,ay,bx,by;
struct NOD
{
int x, y;
int step;
};
int f[8][2]={{-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}};
Queue<nod> Q;
int ans;
void BFs ()
{
Nod T;
while (!q.empty ())
{
T=q.front ();
M[t.x][t.y]=1;
Q.pop ();
if (T.x==bx&&t.y==by)
{
Ans=t.step;
Return
}
for (int i=0;i<8;i++)
{
int xx=t.x+f[i][0];
int yy=t.y+f[i][1];
Nod N;
n.x=xx,n.y=yy,n.step=t.step+1;
if (Xx>=1&&xx<=8&&yy>=1&&yy<=8&&!m[xx][yy])
Q.push (n);
}
}
}
int main ()
{
Char a,c;
int b,d;
while (scanf ("%c%d%c%d", &a,&b,&c,&d)!=eof)//input should be noted that if an input, the middle of a space, you can also use two string format input (space cutoff)
{
GetChar ();
memset (M,0,sizeof (m));
ax=b,ay=a-' a ' +1;
bx=d,by=c-' A ' +1;//sb I repeat definition
cout<<ax<< "<<ay<<" "<<bx<<" "<<by<<endl;
Nod k;
K.x=ax,k.y=ay,k.step=0;
Q.push (k);
BFS ();
cout<< "To get from" <<a<<b<< "to" <<c<<d<< "takes" <<ans<< " Knight moves. " <<endl;
while (!q.empty ())
Q.pop ();//For multiple sets of data, be sure to clear the queue
}
return 0;
}
hdu1372 BFS to find the shortest path length