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. 通过css 实现“瀑布流”

    .hot_list{-webkit-column-count: 2; -moz-column-count: 2; column-count: 2; -moz-column-gap:7px; -webk ...

  2. VS中常用快捷键

    常用的快捷键     这里仅列出一些个人觉得好用的快捷键: 调用智能提示:使用组合键“Ctrl+J” 注释/取消注释: 注释用组合键“Ctrl+K+C” 取消注释用组合键“Ctrl+K+U” 大小写转 ...

  3. SpringWeb项目常用注解简单介绍

    注解分为两类: 一类是使用Bean,即是把已经在xml文件中配置好的Bean拿来用,完成属性.方法的组装:比如@Autowired , @Resource,可以通过byTYPE(@Autowired) ...

  4. vue-14-过滤

    过滤器可以用在两个地方:mustache 插值和 v-bind 表达式 <!-- in mustaches --> {{ message | capitalize }} <!-- i ...

  5. 十六. Python基础(16)--内置函数-2

    十六. Python基础(16)--内置函数-2 1 ● 内置函数format() Convert a value to a "formatted" representation. ...

  6. Flatten Nested List Iterator

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  7. matlab中文本文件与图像转化

    一  将图片转化为txt文本文件 a=imread('picture.bmp');   //读取picture.bmp图片 b=rgb2gray(a);                 //由rgb图 ...

  8. 《图解TCP/IP》读书笔记(转)

    reference: https://www.cnblogs.com/edisonchou/p/5987827.html 一.国际惯例:书托 这是一本图文并茂的网络管理技术书籍,旨在让广大读者理解TC ...

  9. 谷歌开源的TensorFlow Object Detection API视频物体识别系统实现(一)[超详细教程] ubuntu16.04版本

    谷歌宣布开源其内部使用的 TensorFlow Object Detection API 物体识别系统.本教程针对ubuntu16.04系统,快速搭建环境以及实现视频物体识别系统功能. 本节首先介绍安 ...

  10. Java中泛型Class<T>、T与Class<?>

    一.区别 单独的T 代表一个类型 ,而 Class<T>代表这个类型所对应的类, Class<?>表示类型不确定的类 E - Element (在集合中使用,因为集合中存放的是 ...