Knight Moves
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 26102   Accepted: 12305

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 下棋,从一个坐标到另一个坐标最少几步,
第一个输入T个样例
第一行一个数L表示棋盘大小
然后两行两个坐标
 #include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int p[][];
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
memset(p, -, sizeof(p));
queue<int>q;
int L;
scanf("%d", &L);
int a, b, x, y;
scanf("%d%d%d%d", &a, &b, &x, &y);
p[a][b] = ;
q.push(a * + b);//一个坐标转为一个数——前三位是横坐标后三位是纵坐标
while (!q.empty())
{
int t = q.front();
q.pop();
a = t / ;
b = t % ;
if (a == x&&b == y)
{
printf("%d\n", p[a][b]);
break;
}
if (a - >= && b + < L&&p[a - ][b + ] == -)
{
q.push((a - ) * + (b + ));
p[a - ][b + ] = p[a][b] + ;
}//
if (a + < L && b + < L&&p[a + ][b + ] == -)
{
q.push((a + ) * + (b + ));
p[a + ][b + ] = p[a][b] + ;
}//
if (a - >= && b + < L&&p[a - ][b + ] == -)
{
q.push((a - ) * + (b + ));
p[a - ][b + ] = p[a][b] + ;
}//
if (a - >= && b - >=&&p[a - ][b - ] == -)
{
q.push((a - ) * + (b - ));
p[a - ][b - ] = p[a][b] + ;
}//
if (a + <L && b + < L&&p[a + ][b + ] == -)
{
q.push((a + ) * + (b + ));
p[a + ][b + ] = p[a][b] + ;
}//
if (a + <L && b - >=&&p[a + ][b - ] == -)
{
q.push((a + ) * + (b - ));
p[a + ][b - ] = p[a][b] + ;
}//
if (a - >= && b - >=&&p[a - ][b - ] == -)
{
q.push((a - ) * + (b - ));
p[a - ][b - ] = p[a][b] + ;
}//
if (a + <L && b - >=&&p[a + ][b - ] == -)
{
q.push((a + ) * + (b - ));
p[a + ][b - ] = p[a][b] + ;
}//八个可能的位置
}
}
}

超超超简单的bfs——POJ-1915的更多相关文章

  1. 超超超简单的bfs——POJ-3278

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 89836   Accepted: 28175 ...

  2. 转帖: 一份超全超详细的 ADB 用法大全

    增加一句 连接 网易mumu模拟器的方法 adb  connect 127.0.0.1:7555 一份超全超详细的 ADB 用法大全 2016年08月28日 10:49:41 阅读数:35890 原文 ...

  3. 超全超详细的HTTP状态码大全(推荐抓包工具HTTP Analyzer V6.5.3)

    超全超详细的HTTP状态码大全 本部分余下的内容会详细地介绍 HTTP 1.1中的状态码.这些状态码被分为五大类: 100-199 用于指定客户端应相应的某些动作. 200-299 用于表示请求成功. ...

  4. POJ 1915 Knight Moves

    POJ 1915 Knight Moves Knight Moves   Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29 ...

  5. 简单的bfs

    这里主要是写的一个简单的bfs,实例运行了RMAT10无向图,总共有1024个顶点.这种简单的bfs算法存在很明显的缺陷,那就是如果图数据过大,那么进程将会直接被系统杀死. 代码如下: #includ ...

  6. HDU 1253:胜利大逃亡(简单三维BFS)

    pid=1253">胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/ ...

  7. laravel使用redis队列实践(只需6步,超详细,超简单)

    1.配置使用redis队列 在.env文件找到QUEUE_DRIVER=sync改成QUEUE_DRIVER=redis redis配置一般不用改如果有密码改.env文件的REDIS_PASSWORD ...

  8. git 和conding.net 超详细超简单安装

    在做一下操作前,希望你能知道 1.什么是git? 可以参考https://blog.csdn.net/a909301740/article/details/81636662 如果还想多了解一下还可以参 ...

  9. 超!超!超简单,Linux安装Docker

    1.安装依赖yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的 sudo yum install -y yum-utils device-ma ...

随机推荐

  1. 使用CoApp创建NuGet C++静态库包

    NuGet是微软开发平台下的包管理软件,使用它你可以非常方便的将一些第三方的库.框架整合进自己的项目中,省去了不少麻烦的配置过程.但是从官方文档上来看,貌似NuGet对C++的支持不是很好,并且在现阶 ...

  2. MySQL left join操作中 on与where放置条件的区别

    优先级 两者放置相同条件,之所以可能会导致结果集不同,就是因为优先级.on的优先级是高于where的. 1 1 首先明确两个概念: LEFT JOIN 关键字会从左表 (table_name1) 那里 ...

  3. google和oracle闹掰,Java 会不会被抛弃?

    眼花缭乱的编程语言 程序界的语言实在太多,但有一种语言不得不说,那就是java语言,Java语言是Android系统的主要开发语言,现在和Google的关系不是很好,但是他会被淘汰吗?下面简单地分析一 ...

  4. hibernate sql查询转换成VO返回list

    hibernate sql查询转换成VO @Override public List<FenxiVo> getTuanDuiFenxiList(FenxiVo FenxiVo,Intege ...

  5. Web前端总结(小伙伴的)

    以下总结是我工作室的小伙伴的心得,可以参考一下 html+css知识点总结 HTMl+CSS知识点收集 1.letter-spacing和word-spacing的区别 letter-spacing: ...

  6. LVS工作模式与调度算法

    LVS三种工作模式.十种调度算法介绍 工作模式介绍: 1.Virtual server via NAT(VS-NAT) 优点:集群中的物理服务器可以使用任何支持TCP/IP操作系统,物理服务器可以分配 ...

  7. php7 安装yar 生成docker镜像

    Docker包含三个概念:(1)远程仓库即远程镜像库所有镜像的聚集地(不可进入操作).(2)本地镜像即从远程仓库拉取过来的镜像(3)运行起来的本地镜像叫做容器(分层的可操作)Docker使用:1.首先 ...

  8. Http批量异步发送和数据保存

    先说需求. 有个服务程序定时扫描指定文件夹下一个所有文件,文件包含了多个用户(客户)信息及对应的http发送地址和发送数据.现在该服务程序需要提取这些用户信息,然后批量进行发送:发送完后需要将http ...

  9. JavaScript深入浅出补充——(一)数据类型,表达式和运算符

    项目基本做完,在进行下一阶段学习之前先看视频学习回顾一下JavaScript 一.数据类型 JavaScript中有五种原始类型和一种对象类型 JavaScript弱类型语言中隐式转换 num-0 字 ...

  10. Linux 多用户系统

    Linux OS是基于Unix系统开发而来,我们知道计算机是昂贵与稀缺的资源,所以一台计算机就要满足多个用户同时使用,即多用户的系统的思想. 实现方式:通过分时共享的策略.即让多个用户可以同时使用一台 ...