题目链接

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. Java基础-synchronized关键字的用法(转载)

    synchronized--同步 顾名思义是用于同步互斥的作用的. 这里精简的记一下它的使用方法以及意义: 当synchronized修饰 this或者非静态方法或者是一个实例的时候,所同步的锁是加在 ...

  2. OpenCV---边缘保留滤波EPF

    OpenCV经典的两种实现EPF方法:高斯双边和均值迁移 一:双边模糊 差异越大,越会完整保留 def bi_demo(image): dst = cv.bilateralFilter(image,0 ...

  3. 779D. String Game 二分 水

    Link 题意: 给出两字符串$a$,$b$及一个序列,要求从前往后按照序列删掉$a$上的字符,问最少删多少使$b$串不为a的子串 思路: 限制低,直接二分答案,即二分序列位置,不断check即可. ...

  4. Do the Untwist(模拟)

    ZOJ Problem Set - 1006 Do the Untwist Time Limit: 2 Seconds      Memory Limit: 65536 KB Cryptography ...

  5. lazyload support for Zepto.js

    关于lazyload,很久之前整理过它的文档:<Lazy Load(1.7.0)中文文档 -- 延迟加载图片的jQuery插件> 因为懒,所以才要用lazyload.但lazyload用了 ...

  6. [php]手动搭建php开发环境(排错)

    前提:针对自己的系统下载相应的php.apache.mysql,安装完毕后按照以下去配置httpd.conf和php.ini 本人用的是php5.6.4和apache2.4.4 一.Apache : ...

  7. Centos tomcat jmx 远程连接

    jmx配置: -Dcom.sun.management.jmxremote-Dcom.sun.management.jmxremote.authenticate=false-Dcom.sun.mana ...

  8. c++ poco 使用mysql中文乱码问题

    poco 是c++ 一个比较好的库,现在正在学习使用它,碰到一些问题记录在此. poco版本:poco-1.46-all ,带有数据库的支持模块 操作系统:ubuntu 1.使用poco的MySQL模 ...

  9. 【leetcode 简单】第十八题 爬楼梯

    假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解释: 有两 ...

  10. python3学习笔记.1.初体验

    最近工作烦得很 就想找点儿别的事情来做,于是想到了学学python. 因为是vs2017,所以就在里面安装了. 第一个程序肯定是Hello World了. 新建一个python应用程序 代码只有一行 ...