题目:
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、注意搜索方向的个数以及数组的初始化

最新文章

  1. .Net开源微型ORM框架测评
  2. ADV数字的剪切
  3. python走起之第三话
  4. 重新理解JS的6种继承方式
  5. 【转】windows下安装和调用curl的方法
  6. Sprint计划会议1
  7. jquery_ajax
  8. MyEclipse配置进行Hibernate逆映射
  9. TFS 测试用例导入、导出工具
  10. .NET Core多平台项目模板eShopOnContainers编译手记
  11. Struts(十五):主题
  12. 【Objective-C学习笔记】变量和基本的数据类型
  13. 洛谷 P2820 局域网
  14. mnist的格式说明,以及在python3.x和python 2.x读取mnist数据集的不同
  15. [原创] 扩展jquery-treegrid插件, 实现勾选功能和全删按钮.
  16. bash 文件名操作 常用方法
  17. 关于c#连接数据库的代码
  18. 记录 spf13-vim 遇到的问题
  19. 20175310 《Java程序设计》第1周学习总结(1)安装虚拟机
  20. mysql 命令一套

热门文章

  1. UWP 使用OneDrive云存储2.x api(二)【全网首发】
  2. Handwritten Parsers &amp; Lexers in Go (翻译)
  3. Java中读取txt文件中中文字符时,出现乱码的解决办法
  4. XML文件解析数据结构
  5. Python学习_11_类和实例
  6. vue的挖坑和爬坑之vuex的简单入门
  7. python模块安装报错 :error: command &#39;gcc&#39; failed with exit status 1
  8. Windows上最大传输单元MTU值的查看和设置
  9. 浅谈Vue模板的那些事儿
  10. python 中文编码