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

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

所以搜索就有八个方向

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

因为测试数据里面有一组 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. delphi一些小技巧 从别处看到

    开发环境--------    Delphi 7是一个很经典的版本,在Win2000/XP下推荐安装Delphi 7来开发软件,在Vista下推荐使用Delphi 2007开发软件.安装好Delphi ...

  2. codeforces 430A Points and Segments (easy)(理解能力有待提高……)

    题目 //终于看懂题目了,,,, //一条线段里面不是每个坐标上都有要染色的点,所以为了满足条件,只能考虑那些给出坐标的点 //所以就要排序一下了,不能直接根据坐标0 1 0 1…… #include ...

  3. .htaccess的基本作用及相关语法介绍

    .htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令. .htaccess主要的作用有:URL重写.自定义错误页面.MIME类型配置以及访问权限控制等.主要体现在伪静态的应 ...

  4. C# 前台线程与后台线程的区别和联系

    c#前台线程与后台线程的区别和联系http://www.189works.com/article-13702-1.html 如何取消后台线程的执行http://www.cnblogs.com/shan ...

  5. HDU 1026 Ignatius and the Princess I (BFS)

    题目链接 题意 : 从(0,0)点走到(N-1,M-1)点,问最少时间. 思路 : BFS..... #include <stdio.h> #include <string.h> ...

  6. ExtJs布局之accordion,fit,auto

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  7. poj 2480 Longge's problem 积性函数

    思路:首先给出几个结论: 1.gcd(a,b)是积性函数: 2.积性函数的和仍然是积性函数: 3.phi(a^b)=a^b-a^(b-1); 记 f(n)=∑gcd(i,n),n=p1^e1*p2^e ...

  8. Windows 代码实现关机(直接黑屏)

    整理资料的时候发现的以前的代码,本机Win7 x64 Sp1 运行直接关机,黑屏.就是利用RtlAdjustPrivilege函数提权,代码中的注释写的很详细了.用的VS2010写的,直接编译成x64 ...

  9. TestDirector创建域或工程

    一.打开TestDirector 1.打开TestDirector,进入如下页面 点击左上角"Site Administrator"进入 2.在输入框里输入正确的Site Admi ...

  10. 了解python

    Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.Python的文本文件是.py文件 Python的用途: 1.做日常事务,比如自动备份你的MP3 2.可以做网站,很多著名的网站包括 ...