嘟嘟嘟

这题首先直接bfs可定过不了,因此可以先贪心缩小两个点的距离,直到达到某一个较小的范围(我用的是30),再bfs暴力求解。

首先我们求出这两个点的相对距离x, y,这样就相当于从(x, y) 走到(0, 0)。然后贪心时,x, y哪一个大,就-=2,另一个--。注意的是要一直保持x, y都是正的,所以每次去绝对值,这种做法正确性可以保证,因为根据题中给的图,可以知道他有对称性。

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<vector>
#include<cctype>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar(x % + '');
} ll cnt = ; int dx[] = {, -, -, , , , , -, -}, dy[] = {, , , , , -, -, -, -};
bool vis[maxn][maxn];
struct Node
{
int x, y, dis;
};
int bfs(int x, int y)
{
queue<Node> q;
q.push((Node){x, y, });
vis[x][y] = ;
while(!q.empty())
{
Node now = q.front(); q.pop();
for(int i = ; i <= ; ++i)
{
int newx = now.x + dx[i], newy = now.y + dy[i];
if(newx == && newy == ) return now.dis + ;
if(newx >= && newx <= && newy >= && newy <= && !vis[newx][newy])
{
vis[newx][newy] = ;
q.push((Node){newx, newy, now.dis + });
}
}
}
return ;
} int main()
{
int xp = read(), yp = read(), xs = read(), ys = read();
int x = abs(xp - xs), y = abs(yp - ys);
while(x + y > )
{
if(x < y) swap(x, y);
x -= ; y -= ;
cnt++;
x = abs(x); y = abs(y);
}
cnt += bfs(x + , y + ); //防止下标出现负数
write(cnt); enter;
}

[HNOI2006]马步距离的更多相关文章

  1. bzoj1193: [HNOI2006]马步距离

    1193: [HNOI2006]马步距离 Time Limit: 10 Sec  Memory Limit: 162 MB Description 在国际象棋和中国象棋中,马的移动规则相同,都是走&q ...

  2. 1193: [HNOI2006]马步距离

    1193: [HNOI2006]马步距离 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2027  Solved: 915[Submit][Statu ...

  3. [BZOJ1193][HNOI2006]马步距离 大范围贪心小范围爆搜

    1193: [HNOI2006]马步距离 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1988  Solved: 905[Submit][Statu ...

  4. P2060 [HNOI2006]马步距离

    P2060 [HNOI2006]马步距离 数据到百万级别,明显爆搜不行,剪枝也没法剪.先打表.发现小数据内步数比较受位置关系影响,但数据一大就不影响了.大概搜了一个20*20的表把赋值语句打出来.判断 ...

  5. 【bzoj1193】[HNOI2006]马步距离

    [HNOI2006]马步距离 Description Input 只包含4个整数,它们彼此用空格隔开,分别为xp,yp,xs,ys.并且它们的都小于10000000. Output 含一个整数,表示从 ...

  6. bzoj1193: [HNOI2006]马步距离(贪心+bfs)

    1193: [HNOI2006]马步距离 题目:传送门 题解: 毒瘤题... 模拟赛时的一道题,刚开始以为是一道大难题...一直在拼命找规律 结果.... 还是说正解吧: 暴力的解法肯定是直接bfs, ...

  7. BZOJ 1193 [HNOI2006]马步距离:大范围贪心 小范围暴搜

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1193 题意: 给定起点(px,py).终点(sx,sy).(x,y < 100000 ...

  8. [BZOJ1193][HNOI2006]马步距离(贪心+dfs)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1193 分析: 首先小范围可以直接暴力.(其实只要用上题目中的表就行了) 如果范围比较大 ...

  9. 【BZOJ 1193】 [HNOI2006]马步距离

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 原问题可以等价为两个点. 然后其中一个点要移动到另外一个点. 那么我们可以把左下角那个点(对称总是可以得到一个点在左下角)放在原点的 ...

随机推荐

  1. 小程序插入html代码

    转自:https://blog.csdn.net/BetterGG/article/details/81027291 一.首先,html 分 2 种情况: 1.我在做详情页开发的时候发现获取到的数据是 ...

  2. flask中的数据操作

    flask中数据访问: pip install flask-sqlalemy 创建数据: 创建app的工厂 from flask import Flask from flask_sqlalchemy ...

  3. 分布式ID生成器PHP+Swoole实现(下) - 代码实现

    上篇文章主要介绍<实现原理>,这篇看主要代码的编写. 实现IDGenerator类 64位ID由以下元素组成:固定位占2位,时间戳占41位,服务实例数字编号占4位,业务编号占10位,自增i ...

  4. Java 双亲委派模型

    1. Java 类加载器的分类 2. 双亲委派模型 Parents Delegration Model

  5. docker启动容器报错:IPv4 forwarding is disabled. Networking will not work

    报这个错误会导致宿主机以外的pc 访问不了容器 按照网上的解决办法: 在/usr/lib/sysctl.d/00-system.conf文件后加net.ipv4.ip_forward=1 即可 然后重 ...

  6. Java 集合:List(ArrayList,LinkedList)

  7. BZOJ2476: 战场的数目(矩阵快速幂)

    题意 题目链接 Sol 神仙题Orzzz 考虑两边是否有\(1\) 设\(f[i]\)表示周长为\(2i\)的方案数 第一种情况:左侧或右侧有一个1,那么把这个1删去,对应的方案数为\(f[i - 1 ...

  8. MUI框架-08-窗口管理-创建子页面

    MUI框架-08-窗口管理-创建子页面 之前写过这一篇,不知道为什么被删了,我就大概写了,抱歉 创建子页面是为了,页面切换时,外面的页面不动,让 MUI 写出来的页面更接近原生 app 官方文档:ht ...

  9. String和StringBuffer有什么区别

    首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...

  10. CentOS 7 yum 安装 Nginx

    1.添加Nginx到YUM源 添加CentOS 7 Nginx yum资源库,打开终端,使用以下命令: sudo rpm -Uvh http://nginx.org/packages/centos/7 ...