嘟嘟嘟

这题首先直接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. 第6天:数据Array

    数组Array every() 方法测试数组的所有元素是否都通过了指定函数的测试. array.every callback[, thisArg] callback 被调用时传入三个参数:元素值,元素 ...

  2. 四:SpringCloud-Hystrix

    八:Hystrix断路器 1. 概述 1.1 分布式系统面临的问题 ==复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败.== 上图中的请求需要调用A, P, H ...

  3. Spring与Web

    一.定义页面及Servlet 在jsp页面加入以下,避免乱码 <meta charset="utf-8"> <body> <form action=& ...

  4. Excel2010条件格式的位置

    以下是excel2010的条件格式设置方法(英文版) 具体使用方法可以参考 http://office.microsoft.com/zh-cn/excel-help/HA102809768.aspx

  5. java简单数组定义

    public class Shuzu { static int[] array = new int[] { 32, 2, 2, 5, 45, }; int[] array1[] = new int[1 ...

  6. JS读取本地IP地址信息

    HTML <div> <p>点击下面的按钮,查看本地IP信息</p> <span class="span">点击查看</spa ...

  7. 项目经验:GIS<MapWinGIS>建模第二天

    记录下GIS的进展情况

  8. hustoj搭建--常见问题

    环境: Centos6.5   apache2+PHP5+MySQL 设置apache服务器网站根路径(设置之后可通过IP访问OJ) 1. 进入目录/etc/httpd/conf下的httpd.con ...

  9. 【转】使用expect实现shell自动交互

    原文地址:http://www.nginx.cn/1934.html shell脚本需要交互的地方可以使用here文档是实现,但是有些命令却需要用户手动去就交互如passwd.scp 对自动部署免去用 ...

  10. [算法练习]Add Two Numbers

    题目说明: You are given two linked lists representing two non-negative numbers. The digits are stored in ...