[HDU 1372] Knight Moves

港控/mmm° 2021-09-29 03:34 331阅读 0赞

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372

题目描述:

在一个棋盘中,给你两个点的坐标,同时,你有八种走法,当然题目没明确说,但是这种棋盘就是这种规则。

求:从第一个点都第二个点的最少步数;

注意:

1.因为坐标是字母加数字的形式,注意下输入格式

2.如果用scanf读取输入,那么别忘了加!=EOF,否则会报 Output limit error 错误

3. 如果输入坐标的时候全部按字符格式输入的,在转成数字的时候,字母减掉 ‘a’ ,那么数字要减掉 ‘1’ ,这样横纵坐标的范围才能控制在 0-7

  1. 1 //cin字母加数字输入
  2. 2 #include<iostream>
  3. 3 #include<cstring>
  4. 4 #include<queue>
  5. 5 #include<cstdio>
  6. 6 using namespace std;
  7. 7
  8. 8 char c1,c2;
  9. 9 int sxx,exx;
  10. 10 int sx,sy,ex,ey;
  11. 11 int go[8][2] = {
  12. 1,2,2,1,2,-1,1,-2,-1,-2,-2,-1,-2,1,-1,2};
  13. 12 bool vis[10][10];
  14. 13 struct node
  15. 14 {
  16. 15 int x,y;
  17. 16 int count;
  18. 17 };
  19. 18
  20. 19 bool check(node s)
  21. 20 {
  22. 21 if(s.x>=0&&s.x<8&&s.y>=0&&s.y<8)
  23. 22 return 1;
  24. 23 else
  25. 24 return 0;
  26. 25 }
  27. 26 void bfs()
  28. 27 {
  29. 28 queue<node> Q;
  30. 29 node now,nex;
  31. 30 now.x = sx;now.y = sy;
  32. 31 now.count = 0;
  33. 32 vis[now.x][now.y] = 1;
  34. 33 Q.push(now);
  35. 34 while(!Q.empty())
  36. 35 {
  37. 36 now = Q.front();
  38. 37 Q.pop();
  39. 38 if(now.x==ex&&now.y==ey)
  40. 39 {
  41. 40 printf("To get from %c%d to %c%d takes %d knight moves.\n",c1,sxx,c2,exx,now.count);
  42. 41 return ;
  43. 42 }
  44. 43 for(int i=0;i<8;i++)
  45. 44 {
  46. 45 nex.x = now.x + go[i][0];
  47. 46 nex.y = now.y + go[i][1];
  48. 47 if(check(nex)&&!vis[nex.x][nex.y])
  49. 48 {
  50. 49 vis[nex.x][nex.y] = 1;
  51. 50 nex.count = now.count + 1;
  52. 51 Q.push(nex);
  53. 52 }
  54. 53 }
  55. 54 }
  56. 55 cout<<"0"<<endl;
  57. 56 }
  58. 57
  59. 58 int main()
  60. 59 {
  61. 60 while(cin>>c1>>sxx>>c2>>exx)
  62. 61 {
  63. 62 sy = c1 - 'a';
  64. 63 sx = sxx - 1;
  65. 64 ey = c2 - 'a';
  66. 65 ex = exx - 1;
  67. 66 memset(vis,0,sizeof(vis));
  68. 67 bfs();
  69. 68 }
  70. 69 return 0;
  71. 70 }
  72. 1 //scanf字符数组输入
  73. 2 #include<iostream>
  74. 3 #include<cstring>
  75. 4 #include<queue>
  76. 5 #include<cstdio>
  77. 6 using namespace std;
  78. 7
  79. 8 char s1[3],s2[3];
  80. 9 int sx,sy,ex,ey;
  81. 10 int go[8][2] = {
  82. 1,2,2,1,2,-1,1,-2,-1,-2,-2,-1,-2,1,-1,2};
  83. 11 bool vis[10][10];
  84. 12 struct node
  85. 13 {
  86. 14 int x,y;
  87. 15 int count;
  88. 16 };
  89. 17
  90. 18 bool check(node s)
  91. 19 {
  92. 20 if(s.x>=0&&s.x<8&&s.y>=0&&s.y<8)
  93. 21 return 1;
  94. 22 else
  95. 23 return 0;
  96. 24 }
  97. 25 void bfs()
  98. 26 {
  99. 27 queue<node> Q;
  100. 28 node now,nex;
  101. 29 now.x = sx;now.y = sy;
  102. 30 now.count = 0;
  103. 31 vis[now.x][now.y] = 1;
  104. 32 Q.push(now);
  105. 33 while(!Q.empty())
  106. 34 {
  107. 35 now = Q.front();
  108. 36 Q.pop();
  109. 37 if(now.x==ex&&now.y==ey)
  110. 38 {
  111. 39 printf("To get from %s to %s takes %d knight moves.\n",s1,s2,now.count);
  112. 40 return ;
  113. 41 }
  114. 42 for(int i=0;i<8;i++)
  115. 43 {
  116. 44 nex.x = now.x + go[i][0];
  117. 45 nex.y = now.y + go[i][1];
  118. 46 if(check(nex)&&!vis[nex.x][nex.y])
  119. 47 {
  120. 48 vis[nex.x][nex.y] = 1;
  121. 49 nex.count = now.count + 1;
  122. 50 Q.push(nex);
  123. 51 }
  124. 52 }
  125. 53 }
  126. 54 cout<<"0"<<endl;
  127. 55 }
  128. 56
  129. 57 int main()
  130. 58 {
  131. 59 while(scanf("%s%s",s1,s2)!=EOF)
  132. 60 {
  133. 61 sy = s1[0] - 'a';
  134. 62 sx = s1[1] - '1';
  135. 63 ey = s2[0] - 'a';
  136. 64 ex = s2[1] - '1';
  137. 65 memset(vis,0,sizeof(vis));
  138. 66 bfs();
  139. 67 getchar();
  140. 68 }
  141. 69 return 0;
  142. 70 }

转载于:https://www.cnblogs.com/youpeng/p/10242554.html

发表评论

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

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

相关阅读