HDU-1372 knight moves BFS

Source: Internet
Author: User
Knight moves

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

Problem description

A
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
B.

 

Input

The
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
String consisting of a letter (a-h) representing the column and a digit
(1-8) representing the row on the chessboard.

Output

For each test case, print one line saying "to get from xx to YY takes n knight moves .".

Sample inpute2 E4
A1 B2
B2 C3
A1 H8
A1 H7
H8 A1
B1 C3
F6 F6

Sample outputto 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. at the beginning, I didn't know what the question was about. Baidu Baike learned a little about chess at the end. Here we set two points in an 8x8 chessboard, the row number is given by English letters, and the column number is given by numbers. What you need to ask now is how a server guard moves to another point in at least a few steps. BFS, plus eight directions to determine whether to cross-border. The Code is as follows:
#include <cstdio>#include <cstring>#include <queue>#include <cstdlib>using namespace std;char map[25][25], hash[25][25];struct Node{int x, y, step;}info;int dir[8][2]= { -2, 1, 2, 1, 2, -1, -2, -1, 1, 2, 1, -2, -1, 2, -1, -2 };bool legal( int x, int y ){if( x< 1|| x> 8|| y< 1|| y> 8 ){return false;}return true;}int BFS( int sx, int sy ){memset( hash, 0, sizeof( hash ) );info.x= sx, info.y= sy, info.step= 0;queue< Node >q;hash[sx][sy]= 1;q.push( info );while( !q.empty() ){Node pos= q.front();q.pop();if( map[ pos.x ][ pos.y ]== 2 ){return pos.step;}for( int i= 0; i< 8; ++i ){int x= pos.x+ dir[i][0], y= pos.y+ dir[i][1], step= pos.step+ 1;if( legal( x, y )&& !hash[x][y]  ){info.x= x, info.y= y, info.step= step;hash[x][y]= 1;q.push( info );}}}}int main(){char a[3], b[3];while( scanf( "%s %s", a, b )!= EOF ){memset( map, 0, sizeof( map ) );map[ a[0]- 'a'+ 1 ][ a[1]- '0' ]= 1;map[ b[0]- 'a'+ 1 ][ b[1]- '0' ]= 2;printf( "To get from %s to %s takes %d knight moves.\n", a, b, BFS( a[0]- 'a'+ 1, a[1]- '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.