The movement of the horse

Source: Internet
Author: User

Title Description Zzq very fond of chess, one day, he took the "horse" in chess when suddenly thought of a problem:
Given the squares A and B on the two chessboard, how many steps does it take for the horse to jump from A to B at least?
You are now asked to program to solve this problem.

Hint: Chess board for 8 *8 lattice, the horse's rules for walking, each step of the first cross or straight walk a lattice, and then go outward oblique a lattice. The input input contains multiple sets of test data. Each set of inputs consists of two squares, each containing a lowercase letter (a~h) that represents the Board's column number, and an integer (1~8) that represents the line number of the board. Output for each set of inputs, output one line "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.
Test Instructions Description:

Enter start and end coordinates

Calculates and outputs the minimum number of steps from the starting point to the destination according to the horse's special walk.

Problem Solving Ideas:

Search the shortest path between two points in the graph using breadth first.

Code:

1#include <stdio.h>2#include <string.h>3 structNote4 {5     intx,y,s;6 };7 intMain ()8 {9     Charlist[9];Ten     intSx,sy,fx,fy,flag,k,tx,ty; One     structNote q[ -]; A     inthead,tail,a[ One][ One]; -     intnext[8][2]={ -{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}}; the      while(Gets (list)! =NULL) -     { -sx=list[0]-'a'+1; -sy=list[1]-'0'; +fx=list[3]-'a'+1; -fy=list[4]-'0'; +         if(sx==fx&&sy==fy) A         { atprintf"To get from%c%c to%c%c takes 0 Knight moves.\n", list[0],list[1] -, list[3],list[4]); -             Continue; -         } -Head=1; tail=1; -q[tail].x=SX; inq[tail].y=Sy; -q[tail].s=0; toMemset (A,0,sizeof(a)); +a[sx][sy]=1; -tail++; the          *flag=0; $          while(head<tail)Panax Notoginseng         { -              the              for(k=0; k<=7; k++) +             { Atx=q[head].x+next[k][0]; thety=q[head].y+next[k][1]; +                 if(tx<1|| Tx>8|| ty<1|| Ty>8) -                     Continue; $                 if(a[tx][ty]==0) $                 { -a[tx][ty]=1; -q[tail].x=TX; theq[tail].y=Ty; -q[tail].s=q[head].s+1;Wuyitail++; the                 } -                 if(tx==fx&&ty==fy) Wu                 { -flag=1; About                      Break; $                 } -             } -             if(flag==1) -                  Break; Ahead++; +         } theprintf"To get from%c%c to%c%c takes%d Knight moves.\n", list[0],list[1] -, list[3],list[4],q[tail-1].s); $     } the         return 0; the}

Error-Prone analysis:

1, this question although test instructions not say can't repeat walk, but still can't repeat walk, so remember Mark has gone the road

2. Initializing an array of marked marks

3. Note that when the starting and ending coordinates are equal, the output 0

4, the boundary of the Board judgment, the key is 8*8 Square to understand clearly

Sample test:

H1 A4

F1 e6

A1 A2

Sample output:

To get from H1 to A4 takes 4 knight moves.

To get from F1 to E6 takes 4 knight moves.

To get from A1 to A2 takes 3 knight moves.

The movement of the horse

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.