HDU 1372 Knight Moves 题解

本是古典 何须时尚 2021-09-20 08:08 355阅读 0赞

Knight Moves

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

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

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

University of Ulm Local Contest 1996

Recommend

Eddy | We have carefully selected several similar problems for you: 1072 1240 1312 1241 1016

-—————————————————————————————————————————————————————————————————————————————————-

本题为典型的bfs深搜的模板题

本题只要了解bfs算法就能AC

但是要对国际象棋中的马的运动方式熟悉一下就没问题了

下面上我注释了的

谁都能看懂的代码

-—————————————————————————————————————————————————————————————————————————————————-

  1. 1 //Author:LanceYu
  2. 2 #include<iostream>
  3. 3 #include<string>
  4. 4 #include<cstring>
  5. 5 #include<cstdio>
  6. 6 #include<fstream>
  7. 7 #include<iosfwd>
  8. 8 #include<sstream>
  9. 9 #include<fstream>
  10. 10 #include<cwchar>
  11. 11 #include<iomanip>
  12. 12 #include<ostream>
  13. 13 #include<vector>
  14. 14 #include<cstdlib>
  15. 15 #include<queue>
  16. 16 #include<set>
  17. 17 #include<ctime>
  18. 18 #include<algorithm>
  19. 19 #include<complex>
  20. 20 #include<cmath>
  21. 21 #include<valarray>
  22. 22 #include<bitset>
  23. 23 #include<iterator>
  24. 24 #define ll long long
  25. 25 using namespace std;
  26. 26 const double clf=1e-8;
  27. 27 //const double e=2.718281828;
  28. 28 const double PI=3.141592653589793;
  29. 29 const int MMAX=2147483647;
  30. 30 //priority_queue<int>p;
  31. 31 //priority_queue<int,vector<int>,greater<int> >pq;
  32. 32 struct node
  33. 33 {
  34. 34 int x,y,step;
  35. 35 };
  36. 36 queue<node> q;
  37. 37 int dir[8][2]={
  38. {-2,-1},{-1,-2},{-2,1},{-1,2},{
  39. 1,2},{
  40. 1,-2},{
  41. 2,-1},{
  42. 2,1}};//马所能够跳的八个方向记录下来
  43. 38 int vis[8][8];
  44. 39 char temp[3][3];//定义一个字符串用于输入
  45. 40 int change(char c)//字符转数字
  46. 41 {
  47. 42 switch (c)
  48. 43 {
  49. 44 case 'a':return 0;
  50. 45 break;
  51. 46 case 'b':return 1;
  52. 47 break;
  53. 48 case 'c':return 2;
  54. 49 break;
  55. 50 case 'd':return 3;
  56. 51 break;
  57. 52 case 'e':return 4;
  58. 53 break;
  59. 54 case 'f':return 5;
  60. 55 break;
  61. 56 case 'g':return 6;
  62. 57 break;
  63. 58 case 'h':return 7;
  64. 59 break;
  65. 60 case '1':return 0;
  66. 61 break;
  67. 62 case '2':return 1;
  68. 63 break;
  69. 64 case '3':return 2;
  70. 65 break;
  71. 66 case '4':return 3;
  72. 67 break;
  73. 68 case '5':return 4;
  74. 69 break;
  75. 70 case '6':return 5;
  76. 71 break;
  77. 72 case '7':return 6;
  78. 73 break;
  79. 74 case '8':return 7;
  80. 75 break;
  81. 76 }
  82. 77 }
  83. 78 int bfs(int x,int y,int x1,int y1)
  84. 79 {
  85. 80 while(!q.empty())//队列的初始化,全部清空
  86. 81 q.pop();
  87. 82 int i;
  88. 83 q.push(node{x,y,0});
  89. 84 while(!q.empty())
  90. 85 {
  91. 86 node t=q.front();
  92. 87 q.pop();
  93. 88 if(t.x==x1&&t.y==y1)
  94. 89 return t.step;
  95. 90 for(i=0;i<8;i++)
  96. 91 {
  97. 92 int dx=t.x+dir[i][0];
  98. 93 int dy=t.y+dir[i][1];
  99. 94 if(dx>=0&&dy>=0&&dx<8&&dy<8&&!vis[dx][dy])//基本搜索
  100. 95 {
  101. 96 vis[dx][dy]=1;
  102. 97 q.push(node{dx,dy,t.step+1});
  103. 98 }
  104. 99 }
  105. 100 }
  106. 101 return 0;
  107. 102 }
  108. 103 int main()
  109. 104 {
  110. 105 while(scanf("%s%s",temp[0],temp[1])!=EOF)
  111. 106 {
  112. 107 memset(vis,0,sizeof(vis));
  113. 108 int x=change(temp[0][0]);
  114. 109 int y=change(temp[0][1]);
  115. 110 int x1=change(temp[1][0]);
  116. 111 int y1=change(temp[1][1]);//确定首尾点
  117. 112 int ans=bfs(x,y,x1,y1);
  118. 113 printf("To get from %s to %s takes %d knight moves.\n",temp[0],temp[1],ans);//输出
  119. 114 }
  120. 115 return 0;
  121. 116 }

2018-11-16  00:03:31  Author:LanceYu

转载于:https://www.cnblogs.com/lanceyu/p/9966985.html

发表评论

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

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

相关阅读