HDU - 1372 Knight Moves(bfs入门)

刺骨的言语ヽ痛彻心扉 2023-08-17 15:40 207阅读 0赞

HDU - 1372 Knight Moves

题目链接:https://vjudge.net/problem/HDU-1372#author=swust20141567

题目

在象棋王国,尼古拉斯.火山是一匹英俊的马,他非常幸运迎娶了白马王国的公主,他们将度蜜月,你现在是他们的女仆,火山会问你去一些地方最少需要多少步,这么简单的事当然难不倒你。由于火山是一匹马,他的移动方式将会遵守国际象棋马的走法。

输入:

输入包含一个或多个输入样例。每个测试样例将会有两个坐标,表示现在的位置和将要到达的地方,每个坐标包含一个字母(a-h)表示列和一个数字(1-8) 行,这意味这这个象棋王国是一个8* 8的矩形。

Input

输入包含一个或多个输入样例。每个测试样例将会有两个坐标,表示现在的位置和将要到达的地方,每个坐标包含一个字母(a-h)表示列和一个数字(1-8) 行,这意味这这个象棋王国是一个8* 8的矩形。

Output

每一组样例将会输出一段话 “To get from xx to yy takes n knight moves.”,其中xx表示起点,yy表示终点,n为xx到yy的最短步数。

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.

思路:这道题挺坑,不咋懂象棋,百度才知道这道题的马走日走法,一开始以为一个一个走,知道这个就可以进行bfs了

  1. //
  2. // Created by hjy on 2019/7/10.
  3. //
  4. #include<iostream>
  5. #include<queue>
  6. #include<map>
  7. #include<cstring>
  8. #include<cstdio>
  9. using namespace std;
  10. typedef long long ll;
  11. struct node{
  12. int x;
  13. int y;
  14. int dept;
  15. };
  16. int book[10][10];
  17. int dic[8][2]={
  18. {
  19. 1,-2},{
  20. 2,-1},{
  21. 2,1},{
  22. 1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};
  23. int bfs(int stx,int sty,int lax,int lay)
  24. {
  25. queue<node>qu;
  26. node now,next;
  27. now.x=stx;
  28. now.y=sty;
  29. now.dept=0;
  30. book[stx][sty]=1;
  31. qu.push(now);
  32. while(!qu.empty())
  33. {
  34. now=qu.front();
  35. qu.pop();
  36. if (now.x == lax && now.y == lay)
  37. return now.dept;
  38. for(int i=0;i<8;i++) {
  39. next.x = now.x + dic[i][0];
  40. next.y = now.y + dic[i][1];
  41. if (next.x < 0 || next.y < 0 || next.x>=8 || next.y>=8)
  42. continue;
  43. if (next.x>=0&&next.x<8&&next.y>=0&&next.y<8&&!book[next.x][next.y]) {
  44. book[next.x][next.y] = 1;
  45. next.dept=now.dept+1;
  46. qu.push(next);
  47. }
  48. }
  49. }
  50. return 0;
  51. }
  52. int main()
  53. {
  54. char str[5],str1[5];
  55. map<char,int>mp;
  56. mp['a']=0;
  57. mp['b']=1;
  58. mp['c']=2;
  59. mp['d']=3;
  60. mp['e']=4;
  61. mp['f']=5;
  62. mp['g']=6;
  63. mp['h']=7;
  64. while(cin>>str)
  65. {
  66. getchar();
  67. cin>>str1;
  68. memset(book,0,sizeof(book));
  69. int stx=str[1]-'0'-1;
  70. int sty=mp[str[0]];
  71. int lax=str1[1]-'0'-1;
  72. int lay=mp[str1[0]];
  73. //cout<<stx<<" "<<sty<<" "<<lax<<" "<<lay<<endl;
  74. cout<<"To get from "<<str<<" to "<<str1<<" takes "<<bfs(stx,sty,lax,lay)<<" knight moves."<<endl;
  75. }
  76. return 0;
  77. }
  78. /*
  79. e2 e4
  80. a1 b2
  81. b2 c3
  82. a1 h8
  83. a1 h7
  84. h8 a1
  85. b1 c3
  86. f6 f6
  87. */

转载于:https://www.cnblogs.com/Vampire6/p/11167444.html

发表评论

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

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

相关阅读