超超超简单的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 ...
随机推荐
- markdown基础
介绍: markdown是一种可以使用普通文本编译器编写的标记语言,通过简单的标记语法,它可以使普通文本具有一定的格式.说的简单一点,markdown其实就是一种简单的文本,与普通的文本文件(txt文 ...
- SVG制作简单的图形
圆形 circle <svg width="200" height="200" > <circle cx="100" cy ...
- Python读入CIFAR-10数据库
CIFAR-10可以去http://www.cs.toronto.edu/~kriz/cifar.html下载(记得下载python格式) CIFAR-10数据组成: 训练集和测试集分别有50000和 ...
- Binder的工作原理浅析
在Android开发中,Binder主要用于Service中,包括AIDL和Messenger,其中Messenger的底层实现就是AIDL,所以我们这里通过AIDL来分析一下Binder的工作机制. ...
- TSC打印机使用教程终极版
最近公司做一个资产采集的项目,之前做过此类项目,不过没有整理资料,借这次机会写一下,做个记录. 本教程使用的打印机型号:TSC TTP-244 Plus 官方文档 一.TSC打印机安装 1.机 ...
- java--while、do while、for三种循环体
1.for可以记录执行次数: 2.while.do while的i放在sum的后面和for得到的执行次数和结果是一致的. 1.从执行结果来看,放在前面,虽然执行次数和i放在sum的后面是相同,但是结果 ...
- 水题 第三站 HDU Largest prime factor
先写一遍思路,跟素数表很类似吧. 1)从小到大遍历数据范围内的所有数.把包含质因子的数的位置都设成跟质因子的位置相同. 2)同一个数的位置可能被多次复写.但是由于是从小到大遍历,这就保证了最后一次写入 ...
- python编程快速上手之第5章实践项目参考答案
#!/usr/bin/env python3.5 # coding:utf-8 # 5.6.1 # 好玩游戏的物品清单 # 给定一个字典,包含物品名称和数量,并打印出数量对应的物品 dict_stuf ...
- 设置TrackMouseEvent捕获WM_MOUSEHOVER和WM_MOUSELEAVE消息
WM_MOUSEHOVER(非客户区消息为WM_NCMOUSEHOVER)消息表示鼠标在客户区悬浮消息,WM_MOUSELEAVE(非客户区消息为WM_NCMOUSELEAVE)为鼠标离开客户区消息, ...
- java 图片质量压缩
/** * 图片质量压缩 * @param file 要压缩的图片文件 * @param input 文件输入流 * @param quality 压缩质量(0-1) * @author ouyang ...