题目链接

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

题意

输入正方形棋盘的边长、起点和终点的位置,给定棋子的走法,输出最少经过多少步可以从起点走到终点。

思路

经典bfs题目。

代码

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; struct Node
{
int r;
int c;
int steps; Node(int r, int c, int steps):r(r), c(c), steps(steps){}
}; const int N = ;
queue<Node> q;
int dir[][]={ {-, -}, {-, -}, {-, }, {-, }, {, -}, {, -}, {, }, {, } };
int n;
int sr, sc;
int er, ec;
int visit[N][N]; void bfs()
{
while(!q.empty())
{
Node node = q.front();
q.pop();
if(node.r==er && node.c==ec)
{
cout<<node.steps<<endl;
return;
}
for(int i=; i<; i++)
{
int nr = node.r + dir[i][];
int nc = node.c + dir[i][];
if(nr>= && nr<n && nc>= && nc<n && !visit[nr][nc])
{
visit[nr][nc] = ;
q.push(Node(nr, nc, node.steps+));
}
}
}
} int main()
{
//freopen("poj1915.txt", "r", stdin);
int t;
cin>>t;
while(t--)
{
cin>>n;
cin>>sr>>sc;
cin>>er>>ec; memset(visit, , sizeof(visit));
while(!q.empty()) q.pop();
visit[sr][sc] = ;
q.push(Node(sr, sc, ));
bfs();
}
return ;
}

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

  1. HDU 1372 Knight Moves (bfs)

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

  2. poj2243 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...

  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. poj2243 &amp;&amp; hdu1372 Knight Moves(BFS)

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

  6. HDU 1372 Knight Moves(bfs)

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

  7. ZOJ 1091 Knight Moves(BFS)

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

  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. ftp server安装与配置

    http://note.youdao.com/noteshare?id=905513cfcdba7d6a8d2fbdd0874a6259

  2. Kubernetes - Deploy Containers Using YAML

    In this scenario, you'll learn how to use Kubectl to create and launch Deployments, Replication Cont ...

  3. Jquery中find与each方法使用详解

    本文实例讲述了jQuery中find与each方法用法.分享给大家供大家参考.具体如下: 一.find()方法 jquery选择器非常强大,利用css的命名规约,可以更快更方便的找出想要的元素. 图解 ...

  4. 计算方法 -- 解线性方程组直接法(LU分解、列主元高斯消元、追赶法)

    #include <iostream> #include <cstdio> #include <algorithm> #include <cstdlib> ...

  5. bzoj 2844: albus就是要第一个出场 高斯消元

    LINK 题意:看题目不如看样例解释.给出有n个数的集合,对这些子集中的数求异或,升序统计所有子集得到的数(重复会被计入),询问一个数x,问这个数出现的第一个位置 思路:在这里要求一个所有可能出现的异 ...

  6. Python学习笔记(三十三)常用内置模块(2)collections_namedtuple_deque_defaultdict_OrderedDict_Counter

    摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431953239 ...

  7. [转]C/C++作用域详解

    原文地址:http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777433.html 作用域规则告诉我们一个变量的有效范围,它在哪儿创建,在 ...

  8. Android Studio 更新失败的解决办法

    编辑$ANDROID_STUDIO_HOME/bin/ 下的 studio.exe.vmoptions(如果系统用的Ubuntu,文件应该是studio.vmoptions或者如果是64位系统,应该是 ...

  9. 新手应知道的ASP.NET代码编写规范

    1.局部变量的名称要有意义,尽量用对应的英文命名,比如“用户姓名”变量,不要用aa bb cc等来命名,而要使用userName. 2.不要使用单个字母的变量,如i.n.x等.而要使用index.te ...

  10. DHTML中window的使用

    window对象是对浏览器窗口进行操作的对象.以下列出一些常用的对象(三级为对象的方法.属性) |-navigator:是对浏览器信息进行操作的对象 |-history:包含用户浏览过的url信息 | ...