Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7661    Accepted Submission(s): 4567

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.

 
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.
 
#include<cstdio>
#include<cstring>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
char s[3],s2[3];
int vis[10][10];
struct node{
int x,y;
int step_cnt;
};
node now,nex;
node vs,vd;
int check(node v)
{
if(v.x>=1&&v.x<=8&&v.y>=1&&v.y<=8&&!vis[v.x][v.y])
return 1;
else return 0;
}
void bfs()
{
queue<node>que;
vs.step_cnt=0;
que.push(vs);
vis[vs.x][vs.y]=1;
while(!que.empty()){
now=que.front();
que.pop();
if(now.x==vd.x&&now.y==vd.y) return;
nex.x=now.x+2;nex.y=now.y+1;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x+2;nex.y=now.y-1;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x-2;nex.y=now.y+1;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x-2;nex.y=now.y-1;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x+1;nex.y=now.y+2;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x+1;nex.y=now.y-2;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x-1;nex.y=now.y+2;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x-1;nex.y=now.y-2;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} }
}
int main()
{
while(~scanf("%s%s",s,s2))
{
memset(vis,0,sizeof(vis));
vs.x=s[1]-'0';
vs.y=s[0]-96;
vd.x=s2[1]-'0';
vd.y=s2[0]-96;
bfs();
printf("To get from %s to %s takes %d knight moves.\n",s,s2,now.step_cnt);
}
}

给一个棋盘,算出马从一个点走到另一个点的最小步数(马走日)

Source

Knight Moves的更多相关文章

  1. HDU 1372 Knight Moves

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

  2. [宽度优先搜索] HDU 1372 Knight Moves

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

  3. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  4. UVA 439 Knight Moves --DFS or BFS

    简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...

  5. 【POJ 2243】Knight Moves

    题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...

  6. hdu Knight Moves

    这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...

  7. HDU 1372 (搜索方向稍有改变) Knight Moves

    其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...

  8. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

  9. UVA 439 Knight Moves

      // 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...

随机推荐

  1. [Android Pro] How to get recent tasks on Android “L”?

    reference : http://stackoverflow.com/questions/24590533/how-to-get-recent-tasks-on-android-l/2688546 ...

  2. September 28th 2016 Week 40th Wednesday

    Love all, trust a few, do wrong to none. 爱所有人,信任一些人,不妨害任何人. Reading is a way for me to expand my min ...

  3. UITableView的scrollToRowAtIndexPath:atScrollPosition:animated的崩溃

      UITableView的scrollToRowAtIndexPath:atScrollPosition:animated的崩溃 [摘要:reason: '-[UITableView _conten ...

  4. NYOJ题目893十字架

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsYAAAQRCAIAAACl4dlPAAAgAElEQVR4nO3dO1Ljyv834P8myFkIsR

  5. Hadoop 2.5.2 eclipse plugin 编译 win7 集成

      一.hadoop集群环境配置 参考我的前一篇文章(ubuntu + hadoop2.5.2分布式环境配置 http://www.cnblogs.com/huligong1234/p/4136331 ...

  6. win7下python3.4 ImportError: No module named 'MySQLdb'错误解决方法

    首先,安装PyMySQL C:\Users\fnngj>python -m pip install PyMySQL 执行以下命令会报错: ImportError: No module named ...

  7. C#4.0图解教程 - 第24章 反射和特性 – 2.特性

    1.特性 定义 Attribute用来对类.属性.方法等标注额外的信息,贴一个标签(附着物) 通俗:给 类 或 类成员 贴一个标签,就像航空部为你的行李贴一个标签一样 注意,特性 是 类 和 类的成员 ...

  8. HTML5学习之智能表单(二)

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  9. Arch Linux 安装、配置、美化和优化

    国庆假期玩了下Arch Linux,发现这货跟Ubuntu之流相差甚远,甚难调教,而且安裝过程全命令行,会有各种问题,各种知识... --- 安装引导器--- -------------------- ...

  10. sdut 487-3279【哈希查找,sscanf ,map】

    487-3279 Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 题目链接: sdut:   http://acm.sdut.ed ...