Hdu 1372 Knight Moves (simple bfs, thanks !)

Source: Internet
Author: User

Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 4983 Accepted Submission (s): 3046

 

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 to 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 a 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 Input
E2 e4
A1 b2
B2 c3
A1 h8
A1 h7
H8 a1
B1 c3
F6 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.

Source
Universities of Ulm Local Contest 1996


Recommend
Eddy

Question: Is it chess. Tell the start and end points. You can skip at least a few steps.

Analysis: bfs is generally used to find the optimal solution .. However, the array for determining the weight is too small, so I initially thought that the weight cannot be determined... ... Too weak ..!!!!!!!

Thoughts:

1. scanf skips space reading characters .. You don't need to think too much at all. Scanf ("% c", & c1, & c2); has spaces in the middle... In this way, you can scatter.

2. Think about the issue of heavy judgment. What a painful understanding... T ^ T ..... Judge how big the array is to be opened. Pay attention to it every time to avoid making another mistake .....

3. How can you forget that a-'A' + 1 is equal to 1 ?!

4. Start vis [8] [8] of the array. If the array starts from 0, there is no (8, 8) coordinate. The coordinates of this question are processed from 1-n. Naturally, 5th sets of data cannot be used ..

You only need to open vis [10] [10. It is a waste of space for me to open the code to 100. Haha ....

5. Remember to open the weight array a little bigger later !!!!!!!!!

 

Code:

No heavy lifting...

 

#include<cstdio>#include<iostream>#include<cstring>#include<queue>using namespace std;int dx[]={-1,-2,-2,-1,1,2,2,1};int dy[]={-2,-1,1,2,2,1,-1,-2};char s,e;int ss,ee;int vis[8][8];struct node{    int x,y;    int steps;}tt;bool in(node &a){    if(a.x>=1&&a.x<=8&&a.y>=1&&a.y<=8)        return true;    else return false;}int bfs(){    tt.steps=0;    queue<node> q;    while(!q.empty())        q.pop();    q.push(tt);    node cur,next;    while(!q.empty())    {        cur=q.front();        q.pop();        if(cur.y==e-'a'+1&&cur.x==ee)        {            //printf("test: %d\n",cur.steps);            return cur.steps;        }        else        {            for(int i=0;i<8;i++)            {                next.x=cur.x+dx[i];                next.y=cur.y+dy[i];                if(/*!vis[next.x][next.y]&&*/in(next))                {                    if(next.x==ee&&next.y==e-'a'+1)                        return cur.steps+1;                    //vis[next.x][next.y]=1;                    next.steps=cur.steps+1;                    //printf("djw: %d %d %d\n",next.x,next.y,next.steps);                    q.push(next);                }            }        }    }    return 0;}int main(){    while(scanf("%c%d %c%d",&s,&ss,&e,&ee)!=EOF)    {        getchar();        memset(vis,0,sizeof(vis));        //printf("test: %c%d %c%d\n",s,ss,e,ee);        tt.x=ss;        tt.y=s-'a'+1;        vis[tt.x][tt.y]=1;        //printf("%d %d\n",tt.x,tt.y);        //printf("test: %d %d\n",ee,e-'a'+1);        if(s==e&&ss==ee) printf("To get from %c%d to %c%d takes %d knight moves.\n",s,ss,e,ee,0);        else printf("To get from %c%d to %c%d takes %d knight moves.\n",s,ss,e,ee,bfs());    }    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.