poj2243 bfs
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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
Output
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.
题目大意:这道题是建立在poj2488的基础上的,我们已经知道了knight的移动规则,这道题是给了你起点和终点的位置坐标,问你从起点到终点所需要的最小步数。
思路分析:我的第一直觉就是使用bfs,bfs的实现主要是依靠队列来进行实现 ,这时候队列中元素的类型就显得犹为重要,这在我看来是使用bfs所必须要首先考虑
的问题,对于本题而言,在进行bfs时所需要记录的一个是x,一个是y,还有一个是step,所以我选择了结构体类型,如果需要记录的只有两个元素的话使用pair类型
也是可以的:还要有两点需要注意一下,一个是输出格式,一定要一致,否则会一wa到底,另外一个就是题目说明有多组测试实例,在输出一组答案后一定记得把队列
清空,否则会出现错误。
代码:#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
int maps[10][10];
int f[8][2]={{-2,-1},{-2,1},{-1,2},{-1,-2},{1,2},{1,-2},{2,-1},{2,1}};
struct nod
{
int x;
int y;
int step;
};
queue<nod> bfs;
int main()
{
char a,b;
int c,d;
while(cin>>a>>c>>b>>d)
{
nod m,n,p;
memset(maps,0,sizeof(maps));
int tx=b-'a',ty=d-1;
m.x=a-'a',m.y=c-1,m.step=0;
bfs.push(m);
while(!bfs.empty())
{
p=bfs.front();
if(p.x==tx&&p.y==ty)
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",a,c,b,d,p.step);
break;
}
for(int i=0;i<8;i++)
{
n.x=p.x+f[i][0];
n.y=p.y+f[i][1];
n.step=p.step+1;
if(n.x>=0&&n.x<=7&&n.y>=0&&n.y<=7&&!maps[n.x][n.y])
{
maps[n.x][n.y]=1;
bfs.push(n);
}
}
bfs.pop();
}
while(!bfs.empty())
{
bfs.pop();
}
}
return 0;
}
奋斗!前进!
poj2243 bfs的更多相关文章
- poj2243 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...
- poj2243 && hdu1372 Knight Moves(BFS)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...
- 跳马(Knight Moves), ZOJ1091, POJ2243 x
跳马(Knight Moves), ZOJ1091, POJ2243 题目描述: 给定象棋棋盘上两个位置 a 和 b,编写程序,计算马从位置 a 跳到位置 b 所需步数的最小值. 输入描述: 输入文件 ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
随机推荐
- nyoj 最少步数
算法:搜索(深度优先搜索) 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0,0,1,1,0,0,0,1 1,0,1,0,1, ...
- vc 获取当前时间
1.使用CTime类 CString str; //获取系统时间 CTime tm; tm=CTime:: GetCurrentTime_r(); str=tm.Format("现在时间是% ...
- linux 克隆:device eth0 does not seem to be present,delaying initialization
问题: CentOS6以上的版本在虚拟机中进行克隆复制或者一些列copy动作后导致网络无法启动提示:device eth0 does not seem to be present,delaying i ...
- python保留指定文件、删除目录其他文件的功能(1)
由于给客户的发布版本上客户改动了些代码和图片,我们这边给他们更新publish都是增量更新(开发提供更新指定的文件,我们提取出来给客户进行覆盖更新),但有时需要更新的文件较多导致不得不一个一个的进行查 ...
- [TYVJ] P1015 公路乘车
公路乘车 描述 Description 一个特别的单行街道在每公里处有一个汽车站.顾客根据他们乘坐汽车的公里使来付费.例如样例的第一行就是一个费用的单子. 没有一辆车子行驶超过10公里,一个顾客打算行 ...
- js各种进制数之间的转换
计算机中常用的进制数有二进制.八进制.十进制.十六进制 一.十进制 to 其他 var x = 10; // 或定义其他值均可 x.toString(n); // n 代表要转换到的进制,比如n可以为 ...
- apache服务器参数设置
全局参数设置 ServerRoot:服务器根目录 apache安装目录[我的为:/usr/local/apache/] 用于指定apache服务器的配置文件及日志文件存放的根目录.服务器的基础目录,a ...
- Linux 下通过脚本实现远程自动备份
考虑到在本机上备份数据,一旦该机器硬盘出现故障,数据无法取出.远程手动备份数据费时费力且不及时.最好的方法就是通过脚本实现远程自动互备.但远程无论是通过SSH登陆,还是通过scp拷贝文件都需要输入密码 ...
- smarty模板执行原理
为了实现程序的业务逻辑和内容表现页面的分离从而提高开发速度,php 引入了模板引擎的概念,php 模板引擎里面最流行的可以说是smarty了,smarty因其功能强大而且速度快而被广大php web开 ...
- 初识lucene
lucene的介绍网上有好多,再写一遍可能有点多余了. 使用lucene之前,有一系列的疑问 为什么lucene就比数据库快? 倒排索引是什么,他是怎么做到的 lucene的数据结构是什么样的,cpu ...