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