Knight Moves

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 25909 Accepted: 12244

Description

Background

Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?

The Problem

Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.

For people not familiar with chess, the possible knight moves are shown in Figure 1.

Input

The input begins with the number n of scenarios on a single line by itself.

Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, …, l-1}*{0, …, l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.

Output

For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.

Sample Input

3

8

0 0

7 0

100

0 0

30 50

10

1 1

1 1

Sample Output

5

28

0

Source

TUD Programming Contest 2001, Darmstadt, Germany

具体的可能会有DFS的减枝优化,我没有试,仔细一想,正常的DFS应该都会超时吧,用BFS广度优先搜索

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
int mark[400][400];
int next[9][2]= {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
int startx,starty,p,q,len;
struct node {
int x;
int y;
int step;
};
int main() { int t;
cin>>t;
while(t--) {
queue<node> que;
memset(mark,0,sizeof(mark));
if(!que.empty()) que.pop();
cin>>len>>startx>>starty>>p>>q;
mark[startx][starty]=1;
node one;
one.x=startx;
one.y=starty;
one.step=0;
que.push(one);
int flag=0;
while(!que.empty()) {
one=que.front();
que.pop();
if(one.x==p&&one.y==q) {
cout<<one.step<<endl;
break;
}
for(int i=0; i<8; i++) {
node cur;
cur.x=one.x+next[i][0];
cur.y=one.y+next[i][1];
if(cur.x<0||cur.x>len-1||cur.y<0||cur.y>len-1)
continue;
if(mark[cur.x][cur.y]==0) {
cur.step=one.step+1;
mark[cur.x][cur.y]=1;
que.push(cur);
}
}
} }
return 0;
}

AC~~~

BFS广度优先搜索 poj1915的更多相关文章

  1. 0算法基础学算法 搜索篇第二讲 BFS广度优先搜索的思想

    dfs前置知识: 递归链接:0基础算法基础学算法 第六弹 递归 - 球君 - 博客园 (cnblogs.com) dfs深度优先搜索:0基础学算法 搜索篇第一讲 深度优先搜索 - 球君 - 博客园 ( ...

  2. 图的遍历BFS广度优先搜索

    图的遍历BFS广度优先搜索 1. 简介 BFS(Breadth First Search,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...

  3. 算法竞赛——BFS广度优先搜索

    BFS 广度优先搜索:一层一层的搜索(类似于树的层次遍历) BFS基本框架 基本步骤: 初始状态(起点)加到队列里 while(队列不为空) 队头弹出 扩展队头元素(邻接节点入队) 最后队为空,结束 ...

  4. GraphMatrix::BFS广度优先搜索

    查找某一结点的邻居: virtual int firstNbr(int i) { return nextNbr(i, n); } //首个邻接顶点 virtual int nextNbr(int i, ...

  5. 步步为营(十六)搜索(二)BFS 广度优先搜索

    上一篇讲了DFS,那么与之相应的就是BFS.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...

  6. 关于宽搜BFS广度优先搜索的那点事

    以前一直知道深搜是一个递归栈,广搜是队列,FIFO先进先出LILO后进后出啥的.DFS是以深度作为第一关键词,即当碰到岔道口时总是先选择其中的一条岔路前进,而不管其他岔路,直到碰到死胡同时才返回岔道口 ...

  7. [MIT6.006] 13. Breadth-First Search (BFS) 广度优先搜索

    一.图 在正式进入广度优先搜索的学习前,先了解下图: 图分为有向图和无向图,由点vertices和边edges构成.图有很多应用,例如:网页爬取,社交网络,网络传播,垃圾回收,模型检查,数学推断检查和 ...

  8. DFS(深度优先搜索)和BFS(广度优先搜索)

    深度优先搜索算法(Depth-First-Search) 深度优先搜索算法(Depth-First-Search),是搜索算法的一种. 它沿着树的深度遍历树的节点,尽可能深的搜索树的分支. 当节点v的 ...

  9. DFS+BFS(广度优先搜索弥补深度优先搜索遍历漏洞求合格条件总数)--09--DFS+BFS--蓝桥杯剪邮票

    题目描述 如下图, 有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连)  比如,下面两张图中,粉红色所示部分就是合格的剪取.  请你计算,一共有多少 ...

随机推荐

  1. Vmware tools install

    Vmware tools 1◆ 下载 2◆ diagram   Ifcfg-eth0   =====>关闭防火墙 systemctl stop firewalld.service   ===== ...

  2. etymon word flower bee apiary forget out~1

    1● anth   2● flower 花       1● ap   2● bee 3● apiary 养殖场          

  3. hibernate创建构架

    创建hibernate架构: 注意:需要将所需的架包导进去: 二:Java工程的具体结构: 具体代码如下:hibernate.cfg.xml <!DOCTYPE hibernate-config ...

  4. OOP⑸

    1.封装: 继承: extends java只支持单根继承!(一个类只能有一个直接的父类) 是代码重用的一种方式! 将子类共有的属性和方法提取到父类中去! Object:超类/基类==>java ...

  5. 图解中序遍历线索化二叉树,中序线索二叉树遍历,C\C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  6. angular4-自定义组件

    在 Angular 中,我们可以使用 {{}} 插值语法实现数据绑定. 新建组件 $ ng generate component simple-form --inline-template --inl ...

  7. dede织梦调取一二三级栏目名及栏目下的内容列表的方法

    网站根据需要,把地区划成省-市-文章的层级结构,栏目首页需要显示的是 复制代码代码如下: {dede:channelarclist} <!--省显示--> <a href=" ...

  8. 使用DevExpress Reports和PDF Viewer创建AcroForm Designer

    众所周知,交互式表单(AcroForms)是PDF标准的重要组成部分,AcroForms允许开发者和开发者的用户创建可填写的PDF文档.尽管WinForms和WPF PDF Viewers不支持交互式 ...

  9. C Runtime Library、C  Runtime

    C Runtime Library.C Runtime   1)运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些 ...

  10. android lombok 使用

    把get /set / toString/hash/equal等方法从源文件中简化掉直接编译到二进制文件中 地址https://github.com/rzwitserloot/lombok 一 安装l ...