HUD 1372 Knight Moves DFS ()

Source: Internet
Author: User

Knight MovesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission (s): 3929 Accepted Submission (s ): 2448 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 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 wowould 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 unzip H8 a1b1 c3f6 f6 Sample OutputTo get 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 tak Es 0 knight moves. # include <cstdio> # include <string> # include <cstring> using namespace std; const int dir [8] [2] = {1, 2, 1,-2,-1, 2, -1,-,-1,-,-2,-1}; const int N = 10; char struct, ch2, ch3, methane; int x1, y1, x2, y2; int used [N] [N]; void DFS (int x, int y, int t) {if (x> 7 | x <0 | y> 7 | y <0 | used [x] [y] <= t) return; used [x] [y] = t; for (int I = 0; I <8; I ++) {int tx = x + dir [I] [0]; int ty = y + dir [I] [1]; DFS (tx, ty, T + 1) ;}} int main () {while (scanf ("% c", & found, & ch2, & ch3, & methane )! = EOF) {www.2cto.com getchar (); x1 = bytes-'A', y1 = ch2-'1'; x2 = ch3-'A ', y2 = methane-'1'; for (int I = 0; I <8; I ++) for (int j = 0; j <8; j ++) used [I] [j] = 64; DFS (x1, y1, 0); printf ("To get from % c to % c takes % d knight moves. \ n ", clerk, ch2, ch3, methane, used [x2] [y2]);}

Related Article

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.