HDU-1372 Knight Moves (BFS)
Problem Description
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.
Source
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int visit[][];
int x1,y1,x2,y2;
int dir[][]={{-,-},{-,},{-,},{-,-},
{,-},{,},{,},{,-}};
struct node
{
int x,y;
int step;
};
int BFS(int step)
{
if(x1==x2&&y1==y2)
return step;
int i,x,y,s;
queue<node> q;
node top,temp;
top.x=x1,top.y=y1,top.step=;
q.push(top);
while(!q.empty())
{
top=q.front();
q.pop();
for(i=;i<;i++)
{
x=top.x+dir[i][];
y=top.y+dir[i][];
s=top.step;
if(x>&&y>&&x<=&&y<=&&visit[x][y]==)
{
visit[x][y]=;
++s;
if(x==x2&&y==y2)
return s;
temp.x=x;
temp.y=y;
temp.step=s;
q.push(temp);
}
}
}
return -;
}
int main()
{
char str1[],str2[],t;
while(~scanf("%s%s",str1,str2))
{
memset(visit,,sizeof(visit));
x1=str1[]-'a'+;
y1=str1[]-'';
x2=str2[]-'a'+;
y2=str2[]-'';
visit[x1][y1]=;
t=BFS();
printf("To get from %s to %s takes %d knight moves.\n",str1,str2,t);
}
return ;
}
HDU-1372 Knight Moves (BFS)的更多相关文章
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- 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 ...
- 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了两次---@_@) ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDU 1372 Knight Moves (广搜)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- 杭州电 1372 Knight Moves(全站搜索模板称号)
http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others) ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- HDU 1372 Knight Moves(BFS)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
随机推荐
- 【失败】制作CentOS镜像
1.在vmware上安装centos虚拟机,安装过程中设置CPU时,点选 虚拟化Intel VT-x~ 和 虚拟化CPU性能计数器,网络使用NAT模式 2.#mkdir /opt/iso,上传镜像(用 ...
- linux下shell编程示例-获取进程id
今天初步学习了一下linux下的shell编程,简单记录一下测试用例 1.编辑shell脚本文件如下: #!/bin/bashecho "hello bash linux"echo ...
- ABP手机端调用API时的CORS
这个问题其实很早就考虑了,当时因为也没有特别着急去解决这个问题,就一直拖着.... 好吧,拖延症是不好的,所有不懒得做的,终将会逼着你去再很短的时间内去解决问题...实现项目 改写一个已有的webfo ...
- PHP问题
/usr/bin/ld: cannot find -lltdlcollect2: ld returned 1 exit statusmake: *** [libphp5.la] 错误 1 缺少libt ...
- apache配置文件中的项目
对于每个配置项目,有几个要素: 首先是项目名称 其次是配置的语法 再次是配置的默认值 配置所处的配置文件的位置(分区) 配置所在的模块分区(和核心是否紧密) 配置项目所在的模块 所以对于每个配置项目, ...
- 高效的VS调试技巧
本文总结了十个调试技巧,当你使用VS的时候可以节省你很多时间. 1.悬停鼠标查看表达式 调试有时候很有挑战性,当你步入一个函数想看看哪块出错的时候,查看调用栈来想想值是从哪来的.另一些情况下,则需要添 ...
- MySql中常用语句
1.查询语句: SELECT 查询字段 FROM 表名 WHERE 条件 查询字段可以使用 通配符* 字段名 别名(把长的名字命名一个别名,比较短的) 通配符:SELECT * FROM ' ...
- node.js操作mongoDB数据库
链接数据库: var mongo=require("mongodb"); var host="localhost"; var port=mongo.Connec ...
- yii 使用renderPartial调用另外一个控制器的视图
以下由我们在信易网络公司开发项目的时候终结出的一些经验 我们可以使用renderPartial访问存储在不同控制器的视图文件夹中的部分视图文件. 在Yii1.1.3中,我们使用双斜线“//”,程序就会 ...
- linux 中 ‘|’的作用
利用Linux所提供的管道符“|”将两个命令隔开,管道符左边命令的输出就会作为管道符右边命令的输入.连续使用管道意味着第一个命令的输出会作为 第二个命令的输入,第二个命令的输出又会作为第三个命令的输入 ...