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 ...
随机推荐
- 腾讯云主机 MySQL 远程访问配置方法
使用腾讯云主机安装 MySQL 之后,需要通过以下步骤进行配置以实现远程访问,主要分为两大部分 一.服务器端口配置 1.如果你的云主机配置了安全组,如果没有配置安全组就可以直接跳过“步骤1”的操作,否 ...
- WebDriver API 大全
访问某网页地址:driver.get(url) 或 driver.navigate().to(url) 访问上一个访问的网页(模拟单击浏览器的后退按钮)driver.navigate().back ...
- centos7 编译ntopng源码
先安装编译所需的开发工具 yum groupinstall 'Development Tools' yum install tcl yum install libpcap libpcap-devel ...
- Word+PS制作拼音表格
这几天,朋友让帮忙做个拼音表格,使用Word可以直接标注音标,却无法实现小时候那种4线3格,Word的模板只有练习书法的.使用Excel却,无法将拼音标注单独标注到上一单元格(有朋友会VBA的话,帮我 ...
- jquery获取select选中的值
http://blog.csdn.net/renzhenhuai/article/details/19569593 误区: 一直以为jquery获取select中option被选中的文本值,是这样写的 ...
- JS操作css样式用法
//html <div id="div1" style="background:red;"> 修改背景颜色 </div> <but ...
- 阻止form空表单提交----JavaScript
网上看到很不错的阻止form空表单提交 第一种方法 <div class="warp"> <h2>登录到pfan空间</h2> <p> ...
- nmon进行性能分析
在压测的时候,搭配nmon,可以很好的记录机器cpu情况,内存情况 下载 需要下载nmon和nmon analyser,到各自的官网下载. nmon可以根据自己的操作系统版本下载二进制文件,免去安装. ...
- Spark 学习笔记大纲
Spark 内核 第28课:Spark天堂之门解密 (点击进入博客)从 SparkContext 创建3大核心对象开始到注册给 Master 这个过程中的源码鉴赏 第29课:Master HA彻底解密 ...
- [已解决]This dependency was not found: * common/stylus/index.styl in ./src/main.js To install it, you can run: npm install --save common/stylus/index.styl
出现 This dependency was not found: * common/stylus/index.styl in ./src/main.js To install it, you can ...