Knight Moves
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15633 | Accepted: 8706 |
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.
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.
Source
题目意思就是:给你一个国际象棋的棋盘,一个马棋子从a点走到b点的最小距离是多少。
这题可以用很普通的用bfs来解决。但是bfs效率低。
我又去学习了一种新的搜索方式,A*搜索。
就是在搜索的过程中赋予每个点搜索价值,每次先对价值高的点进行讨论,这样可以节省大量时间。
这里附上一个学习A*算法的博客:http://www.cppblog.com/mythit/archive/2009/04/19/80492.aspx
我的代码也借学习于此博客。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN = 10;
const int INF = 0x3f3f3f3f;
int X[9] = {-2,-2,-1,-1,1,1,2,2};
int Y[9] = {1,-1,2,-2,-2,2,1,-1};
int ex,ey;
char str[MAXN];
int vis[MAXN][MAXN];
struct Node
{
int x,y;
int step;
int g,h,f;
/*
这里:
* G = 从起点A,沿着产生的路径,移动到网格上指定方格的移动耗费。
* H = 从网格上那个方格移动到终点B的预估移动耗费
*/
bool operator < (const Node &k) const
{
return f>k.f;
}
} temp;
priority_queue<Node> q;
int manhadun(Node temp)///曼哈顿距离
{
return (abs(ex - temp.x) + abs(ey - temp.y)) *10;
}
void init()
{
M(vis,0);
temp.g = temp.h = temp.f = temp.step = 0;
while(!q.empty()) q.pop();
}
int Astar (Node temp)
{
q.push(temp);
while(!q.empty())
{
Node top = q.top();
q.pop();
vis[top.x][top.y] = true;
if(top.x == ex && top.y == ey)
{
return top.step;
}
for(int i=0; i<8; i++)
{
Node ss;
ss.x = top.x + X[i];
ss.y = top.y + Y[i];
if(ss.x<8&&ss.x>=0&&ss.y<8&&ss.y>=0&&vis[ss.x][ss.y] == 0)
{
// printf("x==%d y==%d\n",ss.x,ss.y);
ss.g = top.g + 23; /// 因为马每次走的是 2^2 + 1 = 5。 根号5*10 约等于23;
ss.h = manhadun(ss);
ss.f = ss.g + ss.h;
ss.step = top.step + 1;
q.push(ss);
}
}
}
}
int main()
{
while(gets(str))
{
init();
temp.x = str[0] - 'a';
temp.y = str[1] - '1';
ex = str[3] - 'a';
ey = str[4] - '1';
temp.h = manhadun(temp);
temp.f = temp.g + temp.h;
int ans = Astar(temp);
printf("To get from %c%c to %c%c takes %d knight moves.\n",temp.x+'a',temp.y+'1',ex+'a',ey+'1',ans);
}
return 0;
}
//0 1 0 1 0
//1 0 0 0 1
//0 0 1 0 0
//1 0 0 0 1
//0 1 0 1 0