[关闭]
@xunuo 2017-01-18T13:13:48.000000Z 字数 2118 阅读 998

Knight Moves------一个简单的bfs

Time Limit:1000MS  Memory Limit:32768KB  64bit IO Format:%I64d & %I64u

BFS

来源:HDU1372 Knight Moves


Description

在象棋王国,尼古拉斯.火山是一匹英俊的马,他非常幸运迎娶了白马王国的公主,他们将度蜜月,你现在是他们的女仆,火山会问你去一些地方最少需要多少步,这么简单的事当然难不倒你。由于火山是一匹马,他的移动方式将会遵守国际象棋马的走法。 输入: 输入包含一个或多个输入样例。每个测试样例将会有两个坐标,表示现在的位置和将要到达的地方,每个坐标包含一个字母(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. int bfs()
  2. {
  3. node now,next;
  4. queue<node>q;
  5. now.x=x1;
  6. now.y=y1;
  7. now.step=0;
  8. vis[x1][y1]=1;
  9. if(now.x==x2&&now.y==y2)
  10. return now.step;
  11. q.push(now);
  12. while(!q.empty())
  13. {
  14. now=q.front();
  15. q.pop();
  16. if(now.x==x2&&now.y==y2)
  17. return now.step;
  18. for(int i=0;i<8;i++)
  19. {
  20. next.x=now.x+dir[i][0];
  21. next.y=now.y+dir[i][1];
  22. if(next.x>0&&next.x<=8&&next.y>0&&next.y<=8&&vis[next.x][next.y]==0)
  23. {
  24. vis[next.x][next.y]=1;
  25. next.step=now.step+1;
  26. q.push(next);
  27. }
  28. }
  29. }
  30. return -1;
  31. }

完整代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. #include<algorithm>
  5. using namespace std;
  6. int dir[8][2]={1,2,-1,2,-1,-2,1,-2,2,1,-2,-1,-2,1,2,-1};//方向数组
  7. int vis[10][10];//标记数组
  8. char a[10],b[10];//存起点终点
  9. int x1,x2,y1,y2;//起点与终点
  10. struct node
  11. {
  12. int x,y,step;
  13. };
  14. int bfs()
  15. {
  16. node now,next;
  17. queue<node>q;
  18. now.x=x1;
  19. now.y=y1;
  20. now.step=0;
  21. vis[x1][y1]=1;
  22. if(now.x==x2&&now.y==y2)
  23. return now.step;
  24. q.push(now);
  25. while(!q.empty())
  26. {
  27. now=q.front();
  28. q.pop();
  29. if(now.x==x2&&now.y==y2)
  30. return now.step;
  31. for(int i=0;i<8;i++)
  32. {
  33. next.x=now.x+dir[i][0];
  34. next.y=now.y+dir[i][1];
  35. if(next.x>0&&next.x<=8&&next.y>0&&next.y<=8&&vis[next.x][next.y]==0)
  36. {
  37. vis[next.x][next.y]=1;
  38. next.step=now.step+1;
  39. q.push(next);
  40. }
  41. }
  42. }
  43. return -1;
  44. }
  45. int main()
  46. {
  47. while((scanf("%s%s",a,b))!=EOF)
  48. {
  49. x1=a[1]-'0';//!注意!字母与数字之间的转换,下同
  50. y1=a[0]-'a'+1;
  51. x2=b[1]-'0';
  52. y2=b[0]-'a'+1;
  53. memset(vis,0,sizeof(vis));
  54. int t=bfs();
  55. printf("To get from %s to %s takes %d knight moves.\n",a,b,t);
  56. }
  57. return 0;
  58. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注