标签:
Knight MovesDescription
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 f6Sample 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.
1 /* 2 By:OhYee 3 Github:OhYee 4 Email:oyohyee@oyohyee.com 5 Blog:http://www.cnblogs.com/ohyee/ 6 7 かしこいかわいい? 8 エリーチカ! 9 要写出来Хорошо的代码哦~ 10 */ 11 12 #include <cstdio> 13 #include <algorithm> 14 #include <cstring> 15 #include <cmath> 16 #include <string> 17 #include <iostream> 18 #include <vector> 19 #include <list> 20 #include <queue> 21 #include <stack> 22 using namespace std; 23 24 //DEBUG MODE 25 #define debug 0 26 27 //循环 28 #define REP(n) for(int o=0;o<n;o++) 29 30 const int maxn = 10; 31 int k[maxn]; 32 33 const int delta[8] = {-1,-1,1,1,-2,2,2,-2}; 34 35 int BFS(int s1,int s2,int v1,int v2) { 36 if(s1 == v1 && s2 == v2) 37 return 0; 38 39 queue<pair<int,int> > Q; 40 bool visited[maxn][maxn]; 41 memset(visited,false,sizeof(visited)); 42 int dis[maxn][maxn]; 43 memset(dis,0,sizeof(dis)); 44 45 Q.push(pair<int,int>(s1,s2)); 46 visited[s1][s2] = true; 47 48 while(!Q.empty()) { 49 int th1 = Q.front().first; 50 int th2 = Q.front().second; 51 Q.pop(); 52 53 //达到终点 54 if(th1 == v1 && th2 == v2) 55 break; 56 57 //拓展节点 58 int next1,next2; 59 for(int i = 0;i < 8;i++) { 60 next1 = th1 + delta[i]; 61 next2 = th2 + delta[7 - i]; 62 if(next1 > 8 || next1 < 1 || next2 > 8 || next2 < 1) 63 continue; 64 if(!visited[next1][next2]) { 65 Q.push(pair<int,int>(next1,next2)); 66 visited[next1][next2] = true; 67 dis[next1][next2] = dis[th1][th2] + 1; 68 } 69 } 70 } 71 72 if(dis[v1][v2]) 73 return dis[v1][v2]; 74 else 75 return -1; 76 } 77 78 bool Do() { 79 char s1,v1; 80 int s2,v2; 81 if(scanf("%c%d %c%d\n",&s1,&s2,&v1,&v2) == EOF) 82 return false; 83 printf("To get from %c%d to %c%d takes %d knight moves.\n", 84 s1,s2,v1,v2,BFS(s1 - ‘a‘ + 1,s2,v1 - ‘a‘ + 1,v2)); 85 return true; 86 } 87 88 int main() { 89 while(Do()); 90 return 0; 91 }
标签:
原文地址:http://www.cnblogs.com/ohyee/p/5389471.html