题目:
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. JAVA NIO学习三:NIO 的非阻塞式网络通信

    紧接着上一章,我们继续来研究NIO,上一章中我们讲了NIO 中最常见的操作即文件通道的操作,但实际上NIO的主要用途还是在于网络通信,那么这个时候就会涉及到选择器,这一章我们就会对其进行讲解操作. 一 ...

  2. mysql 数据表字段修改sql 语句

    1 新增字段 alter table bulletin add citycode varchar(6) not null default 0 [after `id`]; # 城市代码 2 修改字段 a ...

  3. 《深入理解java虚拟机》 - 需要一本书来融汇贯通你的经验(下)

    上一章讲到了类的加载机制,主要有传统派的 双亲委派模型 和 现代主义激进派的 osgi 类加载器.接下来继续. 第8章 虚拟机字节码执行引擎 局部变量表,用于存储方法参数和方法内部定义的局部变量. 操 ...

  4. Python学习_09_模块

    模块 模块是python中的最高组织单元,在物理层面上,模块以文件存储,模块的文件名就是模块的名字.py,每个模块都有自己的名称空间. python按照路径搜索来查找模块文件,在PYTHONPATH环 ...

  5. Ant Design Pro 学习二 新建菜单-布局

    新建布局,注意格式: src/common/nav.js 中增加 { component: dynamicWrapper(app, [], () => import('/path/to/NewL ...

  6. AspNet Core Web 应用程序的启动(有关 Program.cs类/ Startup.cs类 ) 当项目中干掉 Startup.cs 类如何设置启动 配置等等

    .有关怎么创建Core MVC/API 这里就不说了,前段时间的博客有说过: 1.  项目生成后会有如图所示两个类 Program类Startup类 2. Startup类  初始内容 public ...

  7. javascript瀑布流

    哇,瀑布流,是的,不错,不错,真的不错,很好玩的样子,于是自己想玩玩啊,来吧,就玩起. 循序渐进,我这里采用原生的js代码来书写.为了方便大家运行代码,我就全部样式和CSS都写在html里面了,当然还 ...

  8. Java 控制台输入数字 输出乘法表(代码练习)

    最近,回忆了一些刚学习Java时经常练习的一些小练习题.感觉还是蛮有趣的,在回顾时想起好多学习时的经历和坎坷,一道小小的练习题要研究半天,珍重过往,直面未来.下面贡献代码,Java 控制台输入数字 输 ...

  9. ionic2 安装(一)

    1.安装java JDK 2.安装nodejs 3.安装最新版ionic 指令:npm install ionic@latest 4.安装cordova 指令:npm install -g cordo ...

  10. SQL Server-聚焦WHERE Column=@Param OR @Param IS NULL有问题?

    前言 上一篇我们讲完SQL动态查询,本节我们继续来讲解SQL动态查询中存在的问题. SQL动态查询条件筛选过滤 当我们创建存储过程调用存储过程时,若筛选条件有值则过滤,没有值则返回所行记录,类似如下查 ...