其实手写模拟一个队列也挺简单的,尤其是熟练以后。

尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马

所以搜索就有八个方向

对了注意初始化标记数组的时候,不要把起点标记为已走过。

因为测试数据里面有一组 f6 f6,此时样例输出的是0

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; struct Point
{
int x, y;
int steps;
}start, end, qu[]; int vis[][];
int dir[][] = {{, }, {-, }, {, -}, {-, -}, {, }, {-, }, {, -}, {-, -}};
char col1, col2;
int row1, row2;
int head, tail; bool islegal(int x, int y)
{
return (x>= && x< && y>= && y< && (!vis[x][y]));
} void BFS(void)
{
head = , tail = ;
qu[].x = start.x;
qu[].y = start.y;
qu[].steps = ;
while(head < tail)
{
if(qu[head].x == end.x && qu[head].y == end.y)
{
printf("To get from %c%d to %c%d takes %d knight moves.\n", col1, row1, col2, row2, qu[head].steps);
return;
}
for(int i = ; i < ; ++i)
{
int xx = qu[head].x + dir[i][];
int yy = qu[head].y + dir[i][];
if(islegal(xx, yy))
{
qu[tail].x = xx;
qu[tail].y = yy;
qu[tail++].steps = qu[head].steps + ;
vis[xx][yy] = ;
}
}
++head;
}
} int main(void)
{
#ifdef LOCAL
freopen("1372in.txt", "r", stdin);
#endif while(cin >> col1 >> row1 >> col2 >> row2)
{
start.x = row1 - , start.y = col1 - 'a';
end.x = row2 - , end.y = col2 - 'a';
memset(vis, , sizeof(vis));
BFS();
}
return ;
}

代码君

HDU 1372 (搜索方向稍有改变) Knight Moves的更多相关文章

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

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

  2. HDU 1372 Knight Moves(bfs)

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

  3. HDU 1372 Knight Moves

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

  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. 使用Zend OpCache 提高 PHP 5.5+ 性能

    使用Zend OpCache 提高 PHP 5.5+ 性能 作者:admin | 时间:February 28, 2015 | 分类:Linux | 评论:1 评论 PHP 5.5 以后内建了 OpC ...

  2. 通过Docker配置DNS服务器

    1. 概述 DockerHub中的 sameersbn/bind 镜像提供了搭建DNS服务器的功能,本文给出使用该镜像搭建DNS服务器的例子. 2. 环境 DNS服务器主机 IP:10.11.150. ...

  3. HDU 4572 Bottles Arrangement(找规律,仔细读题)

    题目 //找规律,123321123321123321…发现这样排列恰好可以错开 // 其中注意题中数据范围: M是行,N是列,3 <= N < 2×M //则猜测:m,m,m-1,m-1 ...

  4. POJ 2070

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...

  5. JS中 判断null

    以下是不正确的方法: var exp = null; if (exp == null) { alert("is null"); } exp 为 undefined 时,也会得到与 ...

  6. Codeforces Round #335 (Div. 2) D. Lazy Student 贪心

    D. Lazy Student   Student Vladislav came to his programming exam completely unprepared as usual. He ...

  7. dom对象详解--document对象(一)

     document对象 Document对象代表整个html文档,可用来访问页面中的所有元素,是最复杂的一个dom对象,可以说是学习好dom编程的关键所在. Document对象是window对象的一 ...

  8. JMeter监控服务器CPU, 内存,网络数据

    http://wenku.baidu.com/link?url=un5QtWHa-A9kCTeVN0PnU3gDEMri38hYqjc8-skNXTD-v65FMObdq1LxfQDb1I6oIK9k ...

  9. hdu 4559 涂色游戏(对SG函数的深入理解,推导打SG表)

    提议分析: 1 <= N <= 4747 很明显应该不会有规律的,打表发现真没有 按题意应该分成两种情况考虑,然后求其异或(SG函数性质) (1)找出单独的一个(一列中只有一个) (2)找 ...

  10. Tomcat中xml文件引入各种schma xsd问题原理

    1.Tomcat下的xml引入各种xsd约束,其实是为了对应xml中用到的各个标签