Knight moves (hdu1372 BFS template question)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1372

Knight moves

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/others) total submission (s): 6731 accepted submission (s): 4059

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 inpute2 e4a1 b2b2 c3a1 h8a1 492h8 a1b1 c3f6 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.
# Include <iostream> # include <stdio. h> # include <string. h> # include <stdlib. h> using namespace STD; int MV [8] [2] ={{-2,-1}, {-1,-2}, {-2 }, {-1, 2}, {1,-2}, {2,-1}, {1, 2, 1}; int V [9] [9], map [9] [9]; char a [3], B [3]; struct node {int X, Y, ans;} Q [1000001]; void BFS (int x, int y) {int e = 0; int S = 0; memset (v, 0, sizeof (v); struct node T, F; T. X = x; T. y = y; T. ans = 0; V [T. x] [T. y] = 1; Q [E ++] = T; while (S <E) {T = Q [s ++]; If (Map [T. x] [T. y] = 1) {printf ("to get from % s to % s takes % d knight moves. \ n ", a, B, T. ans) ;}for (INT I = 0; I <8; I ++) {f. X = T. X + MV [I] [0]; F. y = T. Y + MV [I] [1]; F. ans = T. ans + 1; if (F. x> = 0 & F. x <8 & F. y> = 0 & F. Y <8 & V [F. x] [F. y] = 0) {q [E ++] = f; V [F. x] [F. y] = 1 ;}}} int main () {While (scanf ("% S % s", a, B )! = EOF) {memset (MAP, 0, sizeof (MAP )); map [B [1]-'0'-1] [B [0]-'a'] = 1; // because a-'0' starts from 0, so a [1]-'0'-1; BFS (A [1]-'0'-1, a [0]-'A');} 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.