传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1372

Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13384    Accepted Submission(s): 7831

Problem Description
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 file 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.
 
Source
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1253 1072 1242 1240 1548 
 
分析:
给你起点和终点坐标,只能往8个方向走(类似马走斜日)
问你最短的步数
最经典的bfs问题
很多问题bfs都是这个模板,只不过剪枝条件发生了变化
第一次写bfs,纪念意义很大啊
code:
#include<bits/stdc++.h>
using namespace std;
#define max_v 10
int G[max_v][max_v];
int dir[][]= {{,-},{,-},{,},{,},{-,},{-,},{-,-},{-,-}};
int step;
int sx,sy,fx,fy;
struct node
{
int x,y,step;
};
void bfs()
{
memset(G,,sizeof(G));
queue<node> q;
node p,next;
p.x=sx;
p.y=sy;
p.step=;
G[p.x][p.y]=;
q.push(p); while(!q.empty())
{
p=q.front();
q.pop(); if(p.x==fx&&p.y==fy)
{
step=p.step;
return ;
} for(int i=; i<; i++)
{
next.x=p.x+dir[i][];
next.y=p.y+dir[i][]; if(next.x>=&&next.y>=&&next.x<=&&next.y<=&&G[next.x][next.y]==)
{
next.step=p.step+;
G[next.x][next.y]=;
q.push(next);
}
}
}
}
int main()
{
char c1,c2;
int y1,y2;
while(~scanf("%c%d %c%d",&c1,&y1,&c2,&y2))
{
getchar();
sx=c1-'a'+;
sy=y1;
fx=c2-'a'+;
fy=y2;
bfs();
printf("To get from %c%d to %c%d takes %d knight moves.\n",c1,y1,c2,y2,step);
}
return ;
}
 

HDU 1372 Knight Moves(最简单也是最经典的bfs)的更多相关文章

  1. HDU 1372 Knight Moves(bfs)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...

  2. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  3. HDU 1372 Knight Moves (广搜)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

  4. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  5. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  6. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

  7. 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 ...

  8. HDOJ/HDU 1372 Knight Moves(经典BFS)

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

  9. (step4.2.1) hdu 1372(Knight Moves——BFS)

    解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...

随机推荐

  1. php之mongodb插入数据后如何返回当前插入记录ID

    <?php /** *插入记录 *参数: *$table_name:表名 *$record:记录 * *返回值: *成功:true *失败:false */ function insert($t ...

  2. 安卓API版本

  3. 项目视图 Project Browser

    项目视图 在这个视图,你可以访问.管理你当前项目资源. 项目视图左侧面板显示项目的文件夹结构的分层列表,当从列表中单击一个文件夹,其内容会显示在面板的右边.你可以点击小三角展开或折叠文件夹,显示他包含 ...

  4. TCP/IP、Http、Soap三个基本的通讯协议

    看到一个说法,比较通俗易懂: HTTP就是邮局的协议,他们规定了你的信封要怎么写,要贴多少邮票等....         SOAP就是你们之间交流的协议,负责把你所需要表达的意思写在信纸上,同时也负责 ...

  5. [转]Tomcat优化之内存、并发、缓存

    1.Tomcat内存优化 Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 catalina.sh 中设置 JAVA_OPTS 参数. JAVA_OPTS ...

  6. Python 显示调用栈

    Python调试不如强类型的语言方便,显示调用栈有时非常必要,inspect模块很好用 import inspect inspect.stack() inspect.stack()返回的是一个函数栈帧 ...

  7. webpack缓存

    缓存 缓存如何工作 1.当缓存客户端需要访问数据时,它首先检查缓存.当在缓存中找到所请求的数据时,它被称为缓存命中. 2.如果在缓存中找不到请求的数据 , 称为缓存未命中的情况,它将从主存储器中提取并 ...

  8. flash8中利用遮罩制作图片切换效果

    http://www.56.com/w73/play_album-aid-8642763_vid-NDY5ODU2Mzg.html

  9. matlab练习程序(单源最短路径Dijkstra)

    图的相关算法也算是自己的一个软肋了,当年没选修图论也是一大遗憾. 图像处理中,也有使用图论算法作为基础的相关算法,比如图割,这个算法就需要求最大流.最小割.所以熟悉一下图论算法对于图像处理还是很有帮助 ...

  10. 仿照jQuery写一个关于选择器的框架(带了注释,请多多指教~)

    var select = (function () { //这是一个异常与捕获的代码,它表示的意思是:如果push方法出现了错误那么就需要重写push方法 try { //这边是自己模拟一个场景,来使 ...