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. cordova.js的坑

    最近在使用cordova开发app,一开始只引用了cordova.js,cordova通过function exec(){}来链接本地和H5,之前代码被下载到了本地,在苹果和android上都能实现功 ...

  2. Java学习笔记--反射API

    反射API 1.反射API的介绍 通过反射API可以获取Java程序在运行时刻的内部结构.比如Java类中包含的构造方法.域和方法等元素,并可以与这些元素进行交换.     按照 一般地面向对象的设计 ...

  3. (1)认识javascript

    认识javascript 在本篇学习资料中,讲解javascript的基本概念.编写工具.在html中的使用: JavaScript 是脚本语言,这门语言可用于 HTML 和 web,更可广泛用于服务 ...

  4. linux开机启动smb服务

    修改/etc/rc.local文件(增加红色部分) [root@localhost ~]# cat /etc/rc.local #!/bin/sh## This script will be exec ...

  5. MySQL 5.7 在windows下修改max_allowed_packet变量

    (一)执行sql遇到的错误如下: ### Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (387 ...

  6. 网络配置之nmcli

    使用nmcli命令配置网络 NetworkManager是管理和监控网络设置的守护进程,设备既就是网络接口,连接是对网络接口的配置,一个网络接口可以有多个连接配置,但同时只有一个连接配置生效. 1 配 ...

  7. 几个SQL语句(备忘)

    1.三涨停 select biao1.代码,biao1.名称 from biao1,biao2,biao3 where (biao1.涨幅+ biao2.涨幅+biao3.涨幅)>0.27 an ...

  8. 基于Node.js的微信JS-SDK后端接口实现

    做了一个网站,放到线上,用微信打开,点击分享,可是分享后发给朋友的链接卡片是微信默认自带的,如下: 这标题,描述以及图片是默认自带的,丑不说,分享给别人还以为是盗号网站呢,而接入微信的JSSDK后,分 ...

  9. Java编程思想总结笔记The first chapter

    总觉得书中太啰嗦,看完总结后方便日后回忆,本想偷懒网上找别人的总结,无奈找不到好的,只好自食其力,尽量总结得最好. 第一章  对象导论 看到对象导论觉得这本书 目录: 1.1 抽象过程1.2 每个对象 ...

  10. CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu?

    CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu? Description (原题来自刘汝佳<训练指南>Pa ...