题目链接

http://poj.org/problem?id=2243

题意

输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径。

思路

使用bfs求解,注意国际象棋中马的走法。

代码

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
using namespace std; struct Node
{
int r, c;
int steps; Node(int r, int c, int s) :r(r), c(c), steps(s) {}
}; const int N = ;
int boasd[N][N];
int visit[N][N];
int dir[][] = { {-, -}, {-, -}, {-, }, {-, }, {, -}, {, -}, {, }, {, } };
string ss, se;
int sr, sc;
int er, ec; void bfs()
{
memset(visit, , sizeof(visit));
queue<Node> q;
q.push(Node(sr, sc, ));
visit[sr][sc] = ;
while (!q.empty())
{
Node node = q.front();
q.pop();
if (node.r == er && node.c == ec)
{
printf("To get from %s to %s takes %d knight moves.\n", ss.c_str(), se.c_str(), node.steps);
return;
}
for (int i = ; i < ; i++)
{
int nr = node.r + dir[i][];
int nc = node.c + dir[i][];
if (nr > && nr <= && nc > && nc <= && !visit[nr][nc])
{
visit[nr][nc] = ;
q.push(Node(nr, nc, node.steps + ));
}
}
}
} int main()
{
//freopen("poj2243.txt", "r", stdin);
while (cin >> ss >> se)
{
sr = ss[] - '';
sc = ss[] - 'a' + ;
er = se[] - '';
ec = se[] - 'a' + ;
bfs();
}
return ;
}

poj2243 Knight Moves(BFS)的更多相关文章

  1. poj2243 &amp;&amp; hdu1372 Knight Moves(BFS)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...

  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. HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏

    Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...

  5. HDU 1372 Knight Moves(bfs)

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

  6. ZOJ 1091 Knight Moves(BFS)

    Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where you are t ...

  7. poj1915 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=1915 题意 输入正方形棋盘的边长.起点和终点的位置,给定棋子的走法,输出最少经过多少步可以从起点走到终点. 思路 经典bfs题目. 代 ...

  8. uva439 - Knight Moves(BFS求最短路)

    题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...

  9. Knight Moves(BFS,走’日‘字)

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

随机推荐

  1. 5种网络通信设计模型(也称IO模型)

    1.基本概念 同步:同步函数一般指调用函数后,等到函数功能实现再返回,期间一直霸占的CPU,等待期间同一个线程无法执行其他函数 异步:异步函数指调用函数后,不管函数功能是否实现,立马返回:通过回调函数 ...

  2. 「Django」rest_framework学习系列-序列化

    序列化方式一 :在业务类里序列化数据库数据 class RolesView(APIView): def get(self,request,*args,**kwargs): roles = models ...

  3. Mac下安装SVN插件javaHL not available的解决方法

    在Mac下安装Eclipse插件svnEclipse插件后,每次打开Eclipse都会弹出如下弹出框: 提示你本机缺少JavaHL Library. 选择Eclipse→偏好设置(preference ...

  4. NOIP模拟赛10

    T1 [HAOI2010]软件安装 https://daniu.luogu.org/problem/show?pid=2515 树上背包,如果有i必须有j,j作为i的父节点 O(nm²) #inclu ...

  5. HDU 1431 思维 基础数论

    找范围内回文素数,最大到1e8,我就是要枚举回文串,再判素数,然后因为这种弱智思路死磕了很久题目. /** @Date : 2017-09-08 15:24:43 * @FileName: HDU 1 ...

  6. bzoj 3622 DP + 容斥

    LINK 题意:给出n,k,有a,b两种值,a和b间互相配对,求$a>b$的配对组数-b>a的配对组数恰好等于k的情况有多少种. 思路:粗看会想这是道容斥组合题,但关键在于如何得到每个a[ ...

  7. display:inline-block之用法

    HTML的元素有多种display属性,比较常见的有display:none; display:block; display:inline和display:inline-block;等.详细可参阅W3 ...

  8. js 给指定ID赋值

    js 给指定ID赋值 <script language="javascript" type="text/javascript"> document. ...

  9. $this->success传递数据

    public function index_edit_doExport() { $search=$_POST['id']; $this->success(U('Tongji/index_edit ...

  10. flume监控一个linux指定的一个文件夹的文件信息

    1.编辑一个配置文件 flume-app.conf  拷贝至fulme的安装目录的conf下 # The configuration file needs to define the sources, ...