HDU 1372 Knight Moves——————BFS , 优先队列

缺乏、安全感 2022-05-14 11:41 272阅读 0赞

Knight Moves

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13742 Accepted Submission(s): 8060

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 would 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 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

  1. e2 e4
  2. a1 b2
  3. b2 c3
  4. a1 h8
  5. a1 h7
  6. h8 a1
  7. b1 c3
  8. f6 f6

Sample Output

  1. To get from e2 to e4 takes 2 knight moves.
  2. To get from a1 to b2 takes 4 knight moves.
  3. To get from b2 to c3 takes 2 knight moves.
  4. To get from a1 to h8 takes 6 knight moves.
  5. To get from a1 to h7 takes 5 knight moves.
  6. To get from h8 to a1 takes 6 knight moves.
  7. To get from b1 to c3 takes 1 knight moves.
  8. To get from f6 to f6 takes 0 knight moves.

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. char s1[5];
  4. char s2[5];
  5. bool vis[10][10];
  6. int d[8][2]={-2,1, -2,-1, -1,2, -1,-2, 1,2, 1,-2, 2,1, 2,-1};
  7. int stx,sty,edx,edy,ans;
  8. struct node{
  9. int x,y,step;
  10. node(){};
  11. node(int _x,int _y,int _step)
  12. {
  13. x=_x; y=_y; step=_step;
  14. }
  15. bool operator <(const node &b)const
  16. {
  17. return step>b.step;
  18. }
  19. };
  20. void bfs(int x,int y)
  21. {
  22. vis[x][y]=1;
  23. node e1,e2;
  24. priority_queue<node> que;
  25. que.push(node(x,y,0));
  26. while(que.size())
  27. {
  28. e1=que.top();que.pop();
  29. if(e1.x==edx && e1.y==edy)
  30. {
  31. ans=e1.step;
  32. break;
  33. }
  34. for(int i=0;i<8;i++)
  35. {
  36. e2.x=e1.x+d[i][0];
  37. e2.y=e1.y+d[i][1];
  38. e2.step=e1.step+1;
  39. if(!vis[e2.x][e2.y] && 0 < e2.x && e2.x < 9 && 0 < e2.y && e2.y < 9 )
  40. que.push(e2);
  41. }
  42. }
  43. }
  44. int main()
  45. {
  46. while(~scanf("%s %s",s1,s2))
  47. {
  48. // printf("%s %s\n",s1,s2);
  49. stx=s1[0]-'a'+1;
  50. sty=s1[1]-'0';
  51. edx=s2[0]-'a'+1;
  52. edy=s2[1]-'0';
  53. memset(vis,0,sizeof(vis));
  54. ans=0;
  55. bfs(stx,sty);
  56. printf("To get from %s to %s takes %d knight moves.\n",s1,s2,ans);
  57. }
  58. return 0;
  59. }

发表评论

表情:
评论列表 (有 0 条评论,272人围观)

还没有评论,来说两句吧...

相关阅读