题目:
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.

题意描述:

输入8*8棋盘样式的起点和终点坐标

计算并输出棋子走“日”字形路线从起点到终点的最少步数

解题思路:

简单的搜索题,可以使用BFS对其进行搜索。

代码实现:

 #include<stdio.h>
#include<string.h>
char map[][];
int bfs(int sx,int sy,int ex,int ey);
struct note
{
int x,y,s;
};
int main()
{
char sch,ech;
int sx,sy,ex,ey;
while(scanf("%c%d %c%d\n",&sch,&sy,&ech,&ey) != EOF)
{
sx=sch-'a'+;
ex=ech-'a'+;
if(sx==ex && sy==ey)
printf("To get from %c%d to %c%d takes 0 knight moves.\n",sch,sy,ech,ey);
else
printf("To get from %c%d to %c%d takes %d knight moves.\n",sch,sy,ech,ey,bfs(sx,sy,ex,ey));
}
return ;
}
int bfs(int sx,int sy,int ex,int ey)
{
struct note que[];
int book[][];
int next[][]={,,,,,-,,-,-,-,-,-,-,,-,};
int head,tail,tx,ty,flag,i,j,k;
head=;
tail=;
que[tail].x=sx;
que[tail].y=sy;
que[tail].s=;
tail++;
memset(book,,sizeof(book));
book[sx][sy]=;
flag=;
while(head<tail)
{
for(k=;k<=;k++)//注意方向个数及数组初始化
{
tx=que[head].x+next[k][];
ty=que[head].y+next[k][]; if(tx < ||tx > ||ty < ||ty > )
continue;
if(book[tx][ty]==)
{
book[tx][ty]=; que[tail].x=tx;
que[tail].y=ty;
que[tail].s=que[head].s+;
tail++;
}
if(tx==ex&&ty==ey)
return que[tail-].s;
}
head++;
}
}

易错分析:

1、注意搜索方向的个数以及数组的初始化

HDU 2243 Knight Moves的更多相关文章

  1. POJ 2243 Knight Moves(BFS)

    POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...

  2. HDU 1372 Knight Moves(最简单也是最经典的bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  3. HDU 1372 Knight Moves(bfs)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...

  4. HDU 1372 Knight Moves

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

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

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

  6. HDU 1372 Knight Moves (bfs)

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

  7. HDU 1372 Knight Moves【BFS】

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

  8. POJ 2243 Knight Moves

    Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13222   Accepted: 7418 Des ...

  9. ZOJ 1091 (HDU 1372) Knight Moves(BFS)

    Knight Moves Time Limit: 2 Seconds      Memory Limit: 65536 KB A friend of you is doing research on ...

随机推荐

  1. CentOS 7.x上gitlab搭建教程(https可用,邮件可用)

    目录 知识要求 搭建感想 搭建过程 参考 知识要求: nginx基础知识 搭建感想 注:以下是我搭建gitlab时的思考,需要nginx的基础知识,Docker的基础知识才容易理解,与下面的搭建过程是 ...

  2. Android 7.1 ActivityManagerService 屏幕旋转流程分析 (四)

    四.Activity的更新(旋转) sendNewConfiguration()会调用到ActivityManagerService的updateConfiguration()来update Conf ...

  3. Sql 两个表left join 查左表最时间最大的一条记录显示

    http://bbs.csdn.net/topics/350135010 参考 select *  from a aa left join b bb on aa.id=bb.cid and bb.ad ...

  4. Android Studio移动鼠标显示悬浮提示的设置方法

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  5. python pandas 合并数据函数merge join concat combine_first 区分

    pandas对象中的数据可以通过一些内置的方法进行合并:pandas.merge,pandas.concat,实例方法join,combine_first,它们的使用对象和效果都是不同的,下面进行区分 ...

  6. jquery.cookie的path坑

    在使用jquery.cookie设置cookie的时候,通常都是直接设置,没有针对path,domain和expires等进行具体的设置,这会导致,同一个cookie的key对应多个value. 1. ...

  7. LeetCode题目总结(一)

    我的代码在github上,https://github.com/WINTERFELLS/LeetCode-Answers 这里只提供个人的解题思路,不一定是最好的. Problems1-20 寻找两个 ...

  8. SQLServer LinkServer 链接服务器

    Linked Server简介 我们日常使用SQL Server数据库时,经常遇到需要在实例Instance01中跨实例访问Instance02中的数据.例如在做数据迁移时,如下语句: insert ...

  9. 利用lsof恢复进程占用的文件

    说明:经常会遇到这种情况,没有使用正确的方式清理进程占用的文件,比如日志.导致空间并没有释放.也有的时候需要恢复进程占用的文件. 解决方式 lsof |grep del # 找出自己要恢复的文件名称. ...

  10. oracle之 dblink 报ORA-03113 TNS-12543

    -- 客户端使用含 dblink sql报错( 症状:当数据库使用dblink访问其他数据库时,第一次执行某条dblink sql大多数情况下可以成功,偶尔执行一段时间后报错.如果该会话空闲二十分钟以 ...