题目链接

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. 修改Tomcat端口

    1.自己的tomcat修改端口号,在tomcat解压缩目录中conf目录下又一个server.xml,将HTTP协议的端口修改为自己想要的即可 <Connector port="808 ...

  2. Java集合框架(set)

    set继承自collection接口,其子类和子接口如下: set的共同特性:不能添加相同的元素,通常无法记住元素添加的顺序 1.HashSet类 判断两元素相同的标准:1.equals方法返回tru ...

  3. Codeforces 807 C. Success Rate

    http://codeforces.com/problemset/problem/807/C C. Success Rate time limit per test 2 seconds memory ...

  4. [转]FILE的用法

    #include <stdio.h> int main() { char c; ; FILE *file; file = fopen("test.txt", " ...

  5. 【POJ】3177 Redundant Paths

    [算法]边双连通分量 [题意&题解]http://blog.csdn.net/geniusluzh/article/details/6619575 (注意第一份代码是错误的) 一些细节: 1. ...

  6. HTTP/2.0 简单总结(转载)

    HTTP/2.0 简单总结(转载于https://linjunzhu.github.io/blog/2016/03/10/http2-zongjie/) 如何使用上 HTTP/2.0 需要浏览器的支持 ...

  7. 解决ajax chrome禁止本地浏览时加载本地其他文件的方法

    在chrome快捷键右键--属性 “ --allow-file-access-from-files ”,前面用空格隔开.然后应用--确定.

  8. vue去除地址栏上的'#'号

    const router = new VueRouter({ routes:[], mode :"history"//除去#号 }

  9. VC进度条的使用

    m_progress->GetPos(); //获取进度条的当前位置 m_progress->GetRange(int min,int max); //获取进度条控件的范围的下限和上限 m ...

  10. 仿360影视网站模板html

    链接:http://pan.baidu.com/s/1mhIkV4s 密码:9wgq