HDU 2243 Knight Moves
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
Output
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的更多相关文章
- POJ 2243 Knight Moves(BFS)
POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...
- HDU 1372 Knight Moves(最简单也是最经典的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- POJ 2243 Knight Moves
Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13222 Accepted: 7418 Des ...
- 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 ...
随机推荐
- 设计模式之 - 代理模式(Proxy Pattern)
代理模式:代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为委托类预处理消息,过滤消息并转发消息,以及进行消息被委托类执行后的后续处理.很多可以框架中都有用 ...
- c# 了解c# 面向对象
C#是微软公司发布的一种面向对象的.运行于.NET Framework之上的高级程序设计语言.并定于在微软职业开发者论坛(PDC)上登台亮相.C#是微软公司研究员Anders Hejlsberg的最新 ...
- ArcGIS API for Javascript 加载天地图(经纬度投影)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 前端之 HTML🎃
HTML这知识点很多很杂,所以整理很乱.所以将就看.
- thinkinginjava学习笔记03_基本语法
由于java是c系语言,基本保留c语言的所有基本操作,就快速过一下: java中的基本操作符仅仅对基本类型有效:=.==.!=对所有对象有效(值传递),String类支持+.+=; 在对基本数据进行算 ...
- Oracle导入导出常用命令
-- 全量导出 exp system/manager@TEST file=d:\daochu.dmp full=y -- 将数据库中system用户与sys用户的表导出 exp system/mana ...
- robotframework的学习笔记(十四)------学习Robot Framework必须掌握的库—-BuiltIn库
作为一门表格语言,为了保持简单的结构,RF没有像别的高级语言那样提供类似if else while等内置关键字来实现各种逻辑功能,而是提供给了用户BuiltIn库.如果用户想在测试用例中实现比较复杂的 ...
- java中重载一定在一个类里面吗?
虽然这些概念在翻译成中文的过程中,有很多不同的翻译方式但本质上只有两种说法,就是Override和Overload其中,Overload一般都被翻译成重载而Override的翻译就乱七八糟了,所谓覆盖 ...
- 【读书笔记】【深入理解ES6】#9-JavaScript中的类
大多数面向对象的编程语言都支持类和类继承的特性,而JavaScript却不支持这些特性,只能通过其他方法定义并关联多个相似的对象.这个状态一直从ECMAScript 1持续到ECMAScript 5. ...
- 【读书笔记】【深入理解ES6】#4-扩展对象的功能性
对象类别 ES6规范清晰定义了每一个类别的对象. 普通(Ordinary)对象 具有JS对象所有的默认内部行为 特异(Exotic)对象 具有某些与默认行为不符的内部行为 标准(Standard)对象 ...