Knight Moves UVA - 439 (BFS)

绝地灬酷狼 2022-02-26 12:43 269阅读 0赞

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.

解题思路:

注意马走日,所以移动方向有8个,使用BFS即可

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<queue>
  5. #include<string.h>
  6. #include<string>
  7. using namespace std;
  8. const int MAXN = 10;
  9. //8种搜索方向
  10. int mmoveX[8] = {2,2,1,1,-2,-2,-1,-1};
  11. int mmoveY[8] = {1,-1,2,-2,1,-1,2,-2};
  12. string srow,scolumn;
  13. bool svisited[MAXN][MAXN]; //是否访问
  14. //广度优先
  15. struct spos{
  16. int x,y;
  17. spos(int _x,int _y){
  18. x = _x,y = _y;
  19. }
  20. };
  21. int main(){
  22. while(cin>>srow>>scolumn){
  23. memset(svisited,false,sizeof(svisited));
  24. int mindis = 0;
  25. int sx = 8-(srow[1]-'0')+1;
  26. int sy = srow[0]-'a'+1;
  27. int ex = 8-(scolumn[1]-'0')+1;
  28. int ey = scolumn[0]-'a'+1;
  29. queue<spos> bfs_queue;
  30. queue<int> layer_queue;
  31. spos sp(sx,sy);
  32. bfs_queue.push(sp);
  33. layer_queue.push(0);
  34. while(!bfs_queue.empty()){ //不为空的话
  35. spos curNode = bfs_queue.front();
  36. int curlayer = layer_queue.front();
  37. bfs_queue.pop();
  38. layer_queue.pop();
  39. if(curNode.x == ex && curNode.y == ey){
  40. mindis = curlayer;
  41. break;
  42. }
  43. svisited[curNode.x][curNode.y] = true;
  44. for(int i = 0; i < 8; ++i){
  45. int newX = curNode.x + mmoveX[i];
  46. int newY = curNode.y + mmoveY[i]; //加边
  47. if(newX >= 1 && newX <= 8 && newY >= 1 && newY <= 8 && !svisited[newX][newY]){
  48. spos newNode(newX,newY);
  49. bfs_queue.push(newNode);
  50. layer_queue.push(curlayer+1); //当前节点+1
  51. }
  52. }
  53. }
  54. cout<<"To get from "<<srow<<" to "<<scolumn<<" takes "<<mindis<<" knight moves."<<endl;
  55. }
  56. system("PAUSE");
  57. return 0;
  58. }

发表评论

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

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

相关阅读