HDU 1372 Knight Moves【BFS】
题意:给出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】的更多相关文章
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- 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 ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDU 1372 Knight Moves(最简单也是最经典的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- HDU 1372 Knight Moves(BFS)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
随机推荐
- eclipse编译项目jar,git管理代码
2015-3-10 settings.xml文件: <localRepository>C:\Users\xxx\.m2\repository</localRepository> ...
- HTTP协议中的5类状态码
① 客户方错误 100 继续 101 交换协议 ② 成功 200 OK 201 已创建 202 接收 ...
- POJ 1953
//FINBONACI数列 #include <iostream> #define MAXN 100 using namespace std; int _m[MAXN]; int main ...
- ubuntu安装 scala
1. 配置路径 sudo gedit /etc/profile 2.在文件后面加入 export PATH=/home/sendi/scala-/bin:$PATH 3.更新 source /etc/ ...
- 深入浅出ES6(十三):类 Class
作者 Jason Orendorff github主页 https://github.com/jorendorff 你可能觉得之前讲解的内容略显复杂,今天我们就讲解一些相对简单的内容,不再是生成器 ...
- HTTP协议header标头详解
本文根据RFC2616(HTTP/1.1规范),参考 http://www.w3.org/Protocols/rfc2068/rfc2068 http://www.w3.org/Protocols/r ...
- UVA 11038 - How Many O's? 计算对答案的贡献
题意: 求[n, m]之间包含0的数字的个数题解:转化为求solve(n) - solve(m-1)的前缀问题 对于求0到n的解,我们举例 n = 25789 对于8这位,让其为0对答案的贡献是 (0 ...
- eclipse创建python项目
http://jingyan.baidu.com/article/19192ad8173300e53f570757.html
- cocos2d游戏界面卡住声音正常播放的问题分析
cocos2d游戏界面卡住声音正常播放的问题分析 从目前已知的情况看,出现这种情况只可能是设备的内存不够导致的. 从代码上来说内存不够时会调用AppController的“- (void)applic ...
- linux下tomcat下部署项目如何打包压缩备份
范例一:将整个 /etc 目录下的文件全部打包成为 /tmp/etc.tar[root@linux ~]# tar -cvf /tmp/etc.tar /etc <==仅打包,不压缩![root ...