Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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.
 
题目大意:
      在一个棋盘上(象棋),水平方向a--h竖直方向1--8,每组样例给定两个坐标,
      求象棋中的 ‘马’ 至少需要几步能够从位置一到达位置二。
解题思路:
      广搜,因为“马走日” 所以马在一个位置上最多能够走八个位置,
      定义一个八个位置的数组,每次搜索八个位置即可。
 
AC代码:
 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std; struct point
{
int x,y;
};
int p[][]; // 标记步数
int sx,sy,dx,dy; // 起始地点 和 终点
int aa[][] = {-,-,-,-,,-,,-,,,,,-,,-,}; // 方向数组
void bfs(int a,int b)
{
memset(p,,sizeof(p));
queue<point > que;
point p1,p2,p3;
p1.x = a; p1.y = b; // 将起始点加入队列
que.push(p1);
p[a][b] = ;
if (a==dx && b==dy) // 如果起始点和终点相同 直接输出
return ;
while (!que.empty())
{
p2 = que.front();
for (int i = ; i < ; i ++)
{
p3.x = p2.x+aa[i][];
p3.y = p2.y+aa[i][];
if (p3.x> && p3.x<= && p3.y> && p3.y<=) // 判断边界
{
if (!p[p3.x][p3.y])
{
que.push(p3);
p[p3.x][p3.y] = p[p2.x][p2.y]+;
if (p3.x == dx && p3.y==dy) // 如果到达终点直接输出
return ;
}
}
}
que.pop();
}
return ;
}
int main ()
{
char s1[],s2[];
while (scanf("%s%s",s1,s2)!=EOF)
{
sx = s1[]-'a'+;
sy = s1[]-'';
dx = s2[]-'a'+;
dy = s2[]-'';
bfs(sx,sy);
printf("To get from %s to %s takes %d knight moves.\n",s1,s2,p[dx][dy]);
}
return ;
}
 

HDU 1372 Knight Moves (bfs)的更多相关文章

  1. HDU 1372 Knight Moves(bfs)

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

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

  3. HDU 1372 Knight Moves(最简单也是最经典的bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  4. HDU 1372 Knight Moves (广搜)

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

  5. HDU 1372 Knight Moves【BFS】

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

  6. poj2243 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...

  7. poj2243 &amp;&amp; hdu1372 Knight Moves(BFS)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...

  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. HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏

    Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...

随机推荐

  1. INSTALLMENT of QValue

    ############################################ INSTALLMENT############################################ ...

  2. O(1) Check Power of 2 - LintCode

    examination questions Using O(1) time to check whether an integer n is a power of 2. Example For n=4 ...

  3. Redis redis-cli常用操作

    一.安装 二.连接 在bin目录下./redis-cli -p port -a password 授权auth password 查看是否连接成功 ping PONG表示连接成功 三.键值相关命令 k ...

  4. (八)open函数的flag详解

    3.1.4.open函数的flag详解13.1.4.1.读写权限:O_RDONLY O_WRONLY O_RDWR(1)linux中文件有读写权限,我们在open打开文件时也可以附带一定的权限说明(譬 ...

  5. iOS中属性与成员变量的区别

    一.类Class中的属性property 在ios第一版中,我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: @interfa ...

  6. Android DisplayMetrics类获取屏幕大小

    DisplayMetrics public class DisplayMetrics   extends Object java.lang.Object     ↳ android.util.Disp ...

  7. 转<%%>、<%=%>、<%$%>、<%@%>的区别

    1. 未定义的命名空间前缀“xsd” 上周在项目开发中遇到这样的一个问题,在一个页面用到了自定义的Picker控件,在IE6.7.8.9以及IE10兼容模式下都没有任何问题,但是一换到IE10时已选择 ...

  8. Xcode 8 的 Debug 新特性

    Contents OverView Static Analyzer Localizability Instance Cleanup Nullablility Runtime Issue View De ...

  9. Android layout_weight的用法

    android:layout_weight是指LinearLayout先给里面的控件分配完大小之后剩余空间的权重. 下面通过举例说明: <LinearLayout xmlns:android=& ...

  10. C#多线程 线程池

    实例1:直接看看微软提供的代码 using System; using System.Threading; public class Example { public static void Main ...