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. 《BI那点儿事》数据流转换——数据转换

    数据转换执行类似于T-SQL中的函数CONVERT或CAST的功能.数据转换的编辑界面如图,选择需要转换的列,在DataType下拉列表中选择需要的数据类型.Output Alias栏内设置输出时使用 ...

  2. javaScript对文字按照拼音排序

    <title>JavaScript对文字按照拼音排序</title> <SCRIPT type="text/javascript"> funct ...

  3. html/CSS基础知识回顾

    html部分 块级元素: 一般用来搭建网站架构,布局,装载内容...像这些大体力的活都属于块级元素.它包括以下标签: address,blockquote,center,dir, div, dl, d ...

  4. redis 数据导出

    一.导出所有的keys echo "keys 201*" |./redis-cli -h localhost -p 6379 -a password >> 1.txt ...

  5. Python中的logging模块

    http://python.jobbole.com/86887/ 最近修改了项目里的logging相关功能,用到了python标准库里的logging模块,在此做一些记录.主要是从官方文档和stack ...

  6. Evolutionary Computing: 3. Genetic Algorithm(2)

    承接上一章,接着写Genetic Algorithm. 本章主要写排列表达(permutation representations) 开始先引一个具体的例子来进行表述 Outline 问题描述 排列表 ...

  7. 面试题2:BAT及各大互联网公司2014前端笔试面试题:HTML/CSS篇

    BAT及各大互联网公司2014前端笔试面试题:HTML/CSS篇 Html篇: 1.你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? IE: trident内核 Firefox:gecko内 ...

  8. 解决WCF的service端无法使用泛型的问题

    思路是将其序列化(比较笨的方法……) 1.引入Newtonsoft.json.dll 2.服务端序列化:return JsonConvert.SerializeObject(result); 3.客户 ...

  9. postgresql中执行计划

    1.Explain explain select * from tablename; 2.explain输出josn格式 explain (format json) select * from tab ...

  10. HDU 1240 Asteroids! 解题报告

    //这道题做完我只有 三个感受  第一:坑: 第二 : 坑! 第三:还是坑! 咳咳  言归正传  WA了无数次之后才发现是输入进去时坐标时z, y, x的顺序输入的 题解   :  类似胜利大逃亡 只 ...