看起来不错,屌屌的。各种游戏寻路。想到星际争霸,帝国时代那些单位恢宏的寻路方式,不得不点赞啊。
Knight Moves
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12244 | Accepted: 6894 |
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 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.
这个题目就是给你象棋中马的起点位置和要去的终点位置,问马到达终点的最短步数,象棋总会下的吧,马走日字。然后就用到上文中的A星算法。f=g+h.按照f从小到大的顺序进行查找。g等于到达的花费,h是目测到终点的距离。有点小题大做的感觉,但是为了学习算法练习下。
#include<limits>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define LL long long
#define eps 1e-8
#define pi acos(-1)
#define INF 0x7fffffff
#define delta 0.98 //模拟退火递增变量
using namespace std;
int X1,X2,Y1,Y2;
int vis[10][10];
int d[8][2]={{-2,-1},{-1,-2},{1,-2},{2,-1},{-2,1},{-1,2},{1,2},{2,1}};
struct point{
int x,y,f,g,h,step;
bool operator <(const point k)const{
return f>k.f;
}
};
int H(point k){
return ((k.x-X2)+(k.y-Y2))*10;
}
int in(point g){
if (g.x<0 || g.x>7 || g.y<0 || g.y>7) return 0;
return 1;
}
priority_queue<point> q;
int ans=0;
void Astar(){
point t,g;
while (!q.empty()){
t=q.top();
q.pop();
vis[t.x][t.y]=1;
if (t.x==X2 && t.y==Y2){
ans=t.step;
return;
}
for (int i=0;i<8;i++){
g.x=t.x+d[i][0];
g.y=t.y+d[i][1];
if (in(g) && !vis[g.x][g.y]){
g.g=t.g+23;
g.h=H(g);
g.f=g.g+g.h;
g.step=t.step+1;
q.push(g);
}
}
}
}
int main(){
char s[5];
point k;
while (gets(s)){
ans=0;
X1=s[0]-'a';Y1=s[1]-'1';
X2=s[3]-'a';Y2=s[4]-'1';
memset(vis,0,sizeof(vis));
k.x=X1;
k.y=Y1;
k.g=k.step=0;
k.h=H(k);
k.f=k.g+k.h;
while (!q.empty()) q.pop();
q.push(k);
Astar();
printf("To get from %c%c to %c%c takes %d knight moves.\n",s[0],s[1],s[3],s[4],ans);
}
return 0;
}