Hdoj topic 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): 7551 accepted submission (s): 4513


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 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.
 
Sourceuniversity of Ulm Local contest 1996
Recommendeddy | we have carefully selected several similar problems for you: 1242 1240 1312 1180 AC code
#include<stdio.h>#include<string.h>#include<queue>#include<string>#include<iostream>#define INF 0xfffffffusing namespace std;int sx,sy,ex,ey;struct s{int x,y,step;}a,temp;int minn,v[10][10];int dx[8]={1,1,2,2,-1,-1,-2,-2};int dy[8]={2,-2,1,-1,-2,2,1,-1};int jud(struct s a){if(a.x<1||a.x>8||a.y<1||a.y>8)return 0;if(v[a.x][a.y])return 0;return 1;}void bfs(){queue<struct s>q;a.x=sx;a.y=sy;a.step=0;q.push(a);v[sx][sy]=1;while(!q.empty()){a=q.front();q.pop();for(int i=0;i<8;i++){temp.x=a.x+dx[i];temp.y=a.y+dy[i];temp.step=a.step+1;if(jud(temp)){if(temp.x==ex&&temp.y==ey){if(temp.step<minn){minn=temp.step;}}v[temp.x][temp.y]=1;q.push(temp);}}}}int main(){char aa,c;int b,d;while(scanf("%c%d %c%d",&aa,&b,&c,&d)!=EOF){getchar();sx=aa-'a'+1;ex=c-'a'+1;sy=b;ey=d;memset(v,0,sizeof(v));minn=INF;bfs();if(minn==INF)minn=0;printf("To get from %c%d to %c%d takes %d knight moves.\n",aa,b,c,d,minn);}}


Hdoj topic 1372 knight moves (BFS)

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.