超超超简单的bfs——POJ-1915
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 26102 | Accepted: 12305 |
Description
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
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
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的更多相关文章
- 超超超简单的bfs——POJ-3278
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 89836 Accepted: 28175 ...
- 转帖: 一份超全超详细的 ADB 用法大全
增加一句 连接 网易mumu模拟器的方法 adb connect 127.0.0.1:7555 一份超全超详细的 ADB 用法大全 2016年08月28日 10:49:41 阅读数:35890 原文 ...
- 超全超详细的HTTP状态码大全(推荐抓包工具HTTP Analyzer V6.5.3)
超全超详细的HTTP状态码大全 本部分余下的内容会详细地介绍 HTTP 1.1中的状态码.这些状态码被分为五大类: 100-199 用于指定客户端应相应的某些动作. 200-299 用于表示请求成功. ...
- POJ 1915 Knight Moves
POJ 1915 Knight Moves Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29 ...
- 简单的bfs
这里主要是写的一个简单的bfs,实例运行了RMAT10无向图,总共有1024个顶点.这种简单的bfs算法存在很明显的缺陷,那就是如果图数据过大,那么进程将会直接被系统杀死. 代码如下: #includ ...
- HDU 1253:胜利大逃亡(简单三维BFS)
pid=1253">胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...
- laravel使用redis队列实践(只需6步,超详细,超简单)
1.配置使用redis队列 在.env文件找到QUEUE_DRIVER=sync改成QUEUE_DRIVER=redis redis配置一般不用改如果有密码改.env文件的REDIS_PASSWORD ...
- git 和conding.net 超详细超简单安装
在做一下操作前,希望你能知道 1.什么是git? 可以参考https://blog.csdn.net/a909301740/article/details/81636662 如果还想多了解一下还可以参 ...
- 超!超!超简单,Linux安装Docker
1.安装依赖yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的 sudo yum install -y yum-utils device-ma ...
随机推荐
- linux下修改Apache配置文件
linux下修改host文件host文件存放路径 /etc/hosts 可以用vim编辑 //Apache配置虚拟主机 /usr/local/apache/conf/extra/httpd-vhost ...
- 查看表结构命令(mysql和oracle)
MySQL查看表结构SQL语句 = mysql查看表结构命令,如下: desc 表名; show columns from 表名; describe 表名; show create table 表名; ...
- bootstrap-datepicker的bug:有日期输入的地方在下个月页面选择当天日期会以当天日期减少一个月显示
Bug复现详细描述:先选择今日日期,然后点击下个月的某个日期,注意,是直接点击下个月的某个日期,不能通过日期显示tab的下个月箭头进入下个月再来点击某个日期,然后再直接点击本月的今日日期.然后bug会 ...
- MOF编译器无法连接VMI服务器。原因可能是语义错误的解决方案
安装数据库时报错. 我这个是因为安装SQL SERVER时,没有卸载vs. 一般解决方法: WIN 7安装VS和SQL SERVER的顺序应该是先安装SQL SERVER 然后安装VS,当要重装SQL ...
- 关于SpringMVC中text/plain的编码导致的乱码问题解决方法
有老铁的项目出现个问题,就是用SpringMVC给前台返回一句话,是String类型的,然后前台接收到是乱码. 然后以为是简单的response的编码问题,就在方法体中开始给response设置编码, ...
- LinkedList源码浅析(jdk1.8)
LinkedList由双向链表实现的集合,因此可以从头或尾部双向循环遍历. LinkedList的操作都是对双向链表的操作,理解双向链表的数据结构就很容易理解LinkedList的实现. 双向链表由带 ...
- docker~从Dockerfile到Container的过程(终于算是OK了)
上一文章,主要介绍Dockerfile里各参数的含义,以及在项目文件里这些内容的含义,因为大叔认为官方和网上其它文章说的有些模棱两可,不太好让大家理解,所有我又从新写了一个大白话的文章,希望可以给大家 ...
- POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)
POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...
- 基于requirejs和angular搭建spa应用
接上篇,angular 实战部分,angular比较适合spa项目,这里不借助任何seed和构建工具,直接从零搭建,基本的angular项目结构大致包含如下几个部分: 1)app.js 入口 2)in ...
- C# 调用.exe文件
process da = new process(); da.startinfo.filename = @""D:\BM0002\BM0002.exe"; //要调用的 ...