题目链接

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. 题解【CF277E Binary Tree on Plane】

    Description 给你平面上 \(n\) 个点 \((2 \leq n \leq 400)\),要求用这些点组成一个二叉树(每个节点的儿子节点不超过两个),定义每条边的权值为两个点之间的欧几里得 ...

  2. opencv函数制作的时钟模型

    http://www.cnblogs.com/sytu/p/4192652.html 在秒针模型的基础上添加了分针和时针,并且添加了暂停控件和设置时间的功能. #include"cv.h&q ...

  3. 手脱tElock 0.98b1 -> tE!

    声明: 只为纪录自己的脱壳历程,高手勿喷 第一种:两次内存法 注: ①这是在win7x32系统上运行的脱壳,所以可能地址不同 ②修复的时候用等级三修复,最后修复不了的剪切掉然后转存合一正常运行,已测试 ...

  4. horizon源码分析(二)

    源码版本:H版 一.简要回顾 对于请求: 地址:/dashboard/admin/instances/ 方式:POST 参数: instances_filter_q: action:instances ...

  5. 关于Linux运维的一些题目总结

    一.有文件file1 1.查询file1里面空行的所在行号 awk ‘{if($0~/^$/)print NR}’ fileorgrep -n ^$ file |awk ‘BEGIN{FS=”:”}{ ...

  6. Hadoop面试链接

    http://blog.csdn.net/haohaixingyun/article/details/52819457 http://blog.csdn.net/kingmax54212008/art ...

  7. vue-router.esm.js?fe87:16 [vue-router] Route with name 'page' does not exist

    本文地址:http://www.cnblogs.com/veinyin/p/7910525.html 我的路由配置 { path: '/page', name: page, component: pa ...

  8. 转:国内优秀npm镜像推荐及使用

    原文:http://riny.net/2014/cnpm/ npm全称Node Package Manager,是node.js的模块依赖管理工具.由于npm的源在国外,所以国内用户使用起来各种不方便 ...

  9. 【leetcode 简单】第十四题 最后一个单词的长度

    给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: &quo ...

  10. layui实现类似于bootstrap的模态框功能

    以前习惯了bootstrap的模态框,突然换了layui,想的用layui实现类似于bootstrap的模态框功能. 用到了layui的layer模块,例如: <!DOCTYPE html> ...