题目链接:http://poj.org/problem?id=2243

Knight Moves
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14500   Accepted: 8108

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

题解:

这题直接用BFS就可以过了。但是想借助这题学一下A*算法(但以下写法好像不是完整的或者说正确的写法,先放一放,以后遇到再深入)。

代码如下:

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; struct node
{
int x, y, step;
int g, h, f;
bool operator<(const node&a)const{
return f>a.f;
}
};
bool vis[MAXN][MAXN];
int dir[][] = { {,}, {,-},{-,},{-,-},{,},{,-},{-,},{-,-} }; int gdis(int x_step, int y_step) //直线距离
{
return (int)sqrt(x_step*x_step+y_step*y_step)*+;
} int hdis(node a, node b) //曼哈顿距离
{
return ( abs(a.x-b.x) + abs(a.y-b.y) )*;
} priority_queue<node>que;
int Astar(node st, node en)
{
memset(vis,false,sizeof(vis));
st.step = st.g = st.h = st.f = ;
while(!que.empty()) que.pop();
que.push(st);
vis[st.x][st.y] = true; while(!que.empty())
{
node now = que.top();
que.pop();
if(now.x==en.x && now.y==en.y)
return now.step; for(int i = ; i<; i++)
{
node tmp;
tmp.x = now.x + dir[i][];
tmp.y = now.y + dir[i][];
if(tmp.x>= && tmp.x<= && tmp.y>= && tmp.y<= && !vis[tmp.x][tmp.y])
{
tmp.step = now.step + ;
tmp.g = now.g + gdis(abs(dir[i][]), abs(dir[i][]));
tmp.h = hdis(tmp, en);
tmp.f = tmp.g + tmp.h; vis[tmp.x][tmp.y] = true;
que.push(tmp);
}
}
}
} int main()
{
char s1[], s2[];
while(scanf("%s%s", s1, s2)!=EOF)
{
node st, en;
st.x = s1[]-'a'+;
st.y = s1[]-'';
en.x = s2[]-'a'+;
en.y = s2[]-'';
int step = Astar(st,en);
printf("To get from %c%d to %c%d takes %d knight moves.\n",st.x+'a'-,st.y,en.x+'a'-,en.y,step);
}
}

POJ2243 Knight Moves —— A*算法的更多相关文章

  1. POJ---2243 Knight Moves 使用A*算法的广度优先搜索

    题目链接:http://poj.org/problem?id=2243 启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省 ...

  2. poj2243 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...

  3. 跳马(Knight Moves), ZOJ1091, POJ2243 x

    跳马(Knight Moves), ZOJ1091, POJ2243 题目描述: 给定象棋棋盘上两个位置 a 和 b,编写程序,计算马从位置 a 跳到位置 b 所需步数的最小值. 输入描述: 输入文件 ...

  4. poj2243 &amp;&amp; hdu1372 Knight Moves(BFS)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...

  5. HDU 1372 Knight Moves 题解

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. Knight Moves (双向bfs)

    # 10028. 「一本通 1.4 例 3」Knight Moves [题目描述] 编写一个程序,计算一个骑士从棋盘上的一个格子到另一个格子所需的最小步数.骑士一步可以移动到的位置由下图给出. [算法 ...

  7. Knight Moves UVA - 439

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...

  8. Knight Moves

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  9. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

随机推荐

  1. N皇后-位运算优化

    N皇后问题 时间限制: 5 Sec  内存限制: 128 MB 题目描述 魔法世界历史上曾经出现过一个伟大的罗马共和时期,出于权力平衡的目的,当时的政治理论家波利比奥斯指出:“事涉每个人的权利,绝不应 ...

  2. 洛谷 [P3265] 装备购买

    线性基 通过题目描述可以感觉到就是要求线性基, 线性基的求法是高斯消元,消完以后剩下的x的系数非 0 的就是线性基 本题有一个贪心策略,每次挑选价格最小的来消掉其他的元 //可以快排预处理 #incl ...

  3. Linux服务器性能分汇总

    工具使用: vmstat 查看cpu时间.内存.IO的情况. 参考:http://linuxcommand.org/man_pages/vmstat8.html 基本用法: [root@root ~] ...

  4. Netflix Ribbon源码设计错误的证据(附正确示例)

    我在之前一篇博客里https://www.cnblogs.com/yangfeiORfeiyang/p/9644254.html 里对Netflix Ribbon的Loadbalancer类源码设计的 ...

  5. 负环 BZOJ 4773

    负环 [问题描述] 在忘记考虑负环之后,黎瑟的算法又出错了.对于边带权的有向图 G = (V, E),请找出一个点数最小的环,使得环上的边权和为负数.保证图中不包含重边和自环. [输入格式] 第1两个 ...

  6. P1067 多项式输出 (模拟)

    题目描述 一元nn次多项式可用如下的表达式表示: 其中,a_i x^i 称为i次项,ai​ 称为i次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式: 多项式中自变量 ...

  7. springboot jetty替换tomcat

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  8. 优秀的数据序列和还原类----TSimpleMsgPack

    优秀的数据序列和还原类----TSimpleMsgPack TSimpleMsgPack是D10天地弦的作品. 优点:至简,就一个单元文件实现,不需要引用其他单元. 缺点:不是标准的MSGPACK实现 ...

  9. 【postgresql】postgresql中的between and以及日期的使用

    在postgresql中的between and操作符作用类似于,是包含边界的 a BETWEEN x AND y 等效于 a >= x AND a <= y 在postgresql中比较 ...

  10. 【Todo】git的fast forward & git命令学习 & no-ff

    git的fast-forward在之前的文章有介绍过,但是介绍的不细: http://www.cnblogs.com/charlesblc/p/5953066.html fast-forward方式就 ...