题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点。

因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形)

另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 105
using namespace std;
struct node
{
int x,y;
int step;
} now,next,st,en;
queue<node>q;
char m,n;
int vis[maxn][maxn];
int dir[][]={{,},{,},{-,},{-,},{-,-},{-,-},{,-},{,-}};
void bfs(int x,int y)
{
now.x=x;now.y=y;now.step=;
while(!q.empty()) q.pop();
q.push(now);
vis[x][y]=;
while(!q.empty())
{
now=q.front();q.pop();
if(now.x==en.x&&now.y==en.y)
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",m,st.y,n,en.y,now.step);
return;
};
for(int i=;i<;i++)
{
next.x=now.x+dir[i][];
next.y=now.y+dir[i][];
if(next.x<||next.y>||next.y<||next.y>) continue;
if(!vis[next.x][next.y])
{
vis[next.x][next.y]=;
next.step=now.step+;
q.push(next);
if(next.x==en.x&&next.y==en.y)
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",m,st.y,n,en.y,next.step);
return;
}
}
}
}
}
int main()
{
while(cin>>m>>st.y>>n>>en.y)
{
memset(vis,,sizeof(vis));
st.x=m-'a'+;
en.x=n-'a'+;
bfs(st.x,st.y);
}
}

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 (bfs)

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

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

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

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

  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. (step4.2.1) hdu 1372(Knight Moves——BFS)

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

  7. HDU 1372 Knight Moves(BFS)

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

  8. HDU 1372 Knight Moves

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

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

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

随机推荐

  1. NGUI 实现 透明底图遮罩 && 人物像素变黑

    今天 UI 那边要求实现一个 透明底图遮罩 与 变黑 的效果. 刚开始考虑使用 shader 实现一个 网上搜了一下,发现了这个,但是底图需要不透明才行,不然他会把 底图的不遮罩部分的透明部分 进行颜 ...

  2. HYSBZ2038 小Z的袜子(莫队算法)

    今天学了一下传说中的解决离线询问不修改的一种算法.题目的意思非常简单,就是询问在一个[L,R]区间里的取两个物品,然后这两个物品颜色相同的概率.其实就是对于每种颜色i,这个区间里对应的个数cnt[i] ...

  3. String Reduction

    问题出自这里 问题描述: Given a string consisting of a,b and c's, we can perform the following operation: Take ...

  4. iOS终端查看.a文件是否能在模拟器上运行

    复制文件路径进去: 红色框框里面没有x86所以模拟器运行会报错

  5. oc和swift的混编

    参考:http://blog.sina.com.cn/s/blog_8d1bc23f0102v5tl.html swift中使用oc类的方法 1.创建一个oc.h文件 2.添加需要倒入的oc类的头文件 ...

  6. mvc5 _ViewStart.cshtml 模板页如何定义

    1._Viewstart.cshtml是一个在呈现View文件的时候的启动文件,会在所有View(.cshtml)被执行之前执行,主要用于一些不方便或不能在母版(_Layout.cshtml)中进行的 ...

  7. ASP 中调用函数关于Call使用注意的问题

    Function TestFun(Tstr) TStr = "Fun2" End Function Sub TestSub(TStr) Tstr = "Sub2" ...

  8. 三、Android NDK编程预备之Java jni入门创建C/C++共享库

    转自: http://www.eoeandroid.com/thread-264971-1-1.html 应网友回复,答应在两天前要出一篇创建C/C++共享库的,但由于清明节假期,跟朋友出去游玩,丢手 ...

  9. Linux网络编程6——使用TCP实现文件服务器

    需求 当客户端连接上服务器后,服务器会将相应文件传输给客户端,实现文件下载. 思路 服务器端,主进程负责listen.循环内,主进程每从任务请求队列中accept出一个请求,就fork出孙子完成文件传 ...

  10. Codeforces Round #336 (Div. 2) B. Hamming Distance Sum 计算答案贡献+前缀和

    B. Hamming Distance Sum   Genos needs your help. He was asked to solve the following programming pro ...