hdu 1372Knight Moves
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Description
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
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.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream> using namespace std; int startx,starty,endx,endy;
char a[3],c[3];
int b[100][100]; int dir[8][2]= {1,2,2,1,2,-1,1,-2,-1,-2,-2,-1,-2,1,-1,2};//往八个方向搜 struct node
{
int x,y,step;
}; void bfs(int x,int y)
{
node st,ed;
memset(b,0,sizeof(b));
queue<node>q;
st.x=x;
st.y=y;
st.step=0;
b[st.x][st.y]=1;
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
if(st.x==endx&&st.y==endy)
{
printf("To get from %s to %s takes %d knight moves.\n",a,c,st.step);
return;
}
for(int k=0; k<8; k++)
{
ed.x=st.x+dir[k][0];
ed.y=st.y+dir[k][1];
if(ed.x>=1&&ed.x<=8&&ed.y>=1&&ed.y<=8&&!b[ed.x][ed.y])
{
b[ed.x][ed.y]=1;
ed.step=st.step+1;
q.push(ed);
}
}
}
} int main()
{
while(~scanf("%s%s",a,c))
{
startx=a[0]-'a'+1;//把字母转化为数字模拟地图
starty=a[1]-'0';
endx=c[0]-'a'+1;
endy=c[1]-'0';
bfs(startx,starty);
}
return 0;
}
hdu 1372Knight Moves的更多相关文章
- hdu Knight Moves
这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...
- 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
其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- 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 ...
随机推荐
- input美化问题
对于前端来说,一个比较大的问题是,当HTML提供的最原始的控件长的很丑可是又没有相应的属性去更改,碰到这个问题时我们应该怎么办?比如radio的那个单选框我觉得好丑啊,怎么办呢?一个很好的思路就是把它 ...
- [linux]安装code::blocks
1.安装基本编译环境 $sudo apt-get install build-essential $sudo apt-get install gdb 2.安装codeblock $sudo apt-g ...
- LintCode 406: Minimum Size
LintCode 406: Minimum Size 题目描述 给定一个由 n 个整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 样例 给 ...
- 【NOIP】提高组2014
Day1 T1(暴力):大水题 #include<cstdio> ][]={ ,,,,, ,,,,, ,,,,, ,,,,, ,,,,, }; ],b[]; int main() { in ...
- 关于angularJS的一些用法
AngularJS 事件指令: ng-click/dblclick ng-mousedown/up ng-mouseenter/leave ng-mousemove/over/out ng-keydo ...
- python3爬虫.2.伪装浏览器
有的网页在爬取时候会报错返回 urllib.error.HTTPError: HTTP Error 403: Forbidden 这是网址在检测连接对象,所以需要伪装浏览器,设置User Agent ...
- Lithium中关键特性更新
Lithium中关键特性更新 1. Lithium特性更新概述 Lithium相对于Helium更新特性共27项,其中原有特性提升或增强13项,新增特性14项,如下表所示 特性类型 相对于Helium ...
- 商城项目(ssm+dubbo+nginx+mysql统合项目)总结(2)
我不会在这里贴代码和详细步骤什么的,我觉得就算我把它贴出来,你们照着步骤做还是会出很多问题,我推荐你们去看一下黑马的这个视频,我个人感觉很不错,一步一步走下来可以学到很多东西.另外,视频和相关文档的话 ...
- django【ORM】 通过外键字段找对应类
两个方法其实是一种,用哪个都行,看实例: 方法一: 从list_filter中的字符串,找到model对象的字段,然后得到这个外键对应的类 循环,把list_filter中对应的类所有对象 方法二 ...
- 基于scrapy的分布式爬虫抓取新浪微博个人信息和微博内容存入MySQL
为了学习机器学习深度学习和文本挖掘方面的知识,需要获取一定的数据,新浪微博的大量数据可以作为此次研究历程的对象 一.环境准备 python 2.7 scrapy框架的部署(可以查看上一篇博客的简 ...