[HNOI2006]马步距离
这题首先直接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]马步距离的更多相关文章
- bzoj1193: [HNOI2006]马步距离
1193: [HNOI2006]马步距离 Time Limit: 10 Sec Memory Limit: 162 MB Description 在国际象棋和中国象棋中,马的移动规则相同,都是走&q ...
- 1193: [HNOI2006]马步距离
1193: [HNOI2006]马步距离 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2027 Solved: 915[Submit][Statu ...
- [BZOJ1193][HNOI2006]马步距离 大范围贪心小范围爆搜
1193: [HNOI2006]马步距离 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1988 Solved: 905[Submit][Statu ...
- P2060 [HNOI2006]马步距离
P2060 [HNOI2006]马步距离 数据到百万级别,明显爆搜不行,剪枝也没法剪.先打表.发现小数据内步数比较受位置关系影响,但数据一大就不影响了.大概搜了一个20*20的表把赋值语句打出来.判断 ...
- 【bzoj1193】[HNOI2006]马步距离
[HNOI2006]马步距离 Description Input 只包含4个整数,它们彼此用空格隔开,分别为xp,yp,xs,ys.并且它们的都小于10000000. Output 含一个整数,表示从 ...
- bzoj1193: [HNOI2006]马步距离(贪心+bfs)
1193: [HNOI2006]马步距离 题目:传送门 题解: 毒瘤题... 模拟赛时的一道题,刚开始以为是一道大难题...一直在拼命找规律 结果.... 还是说正解吧: 暴力的解法肯定是直接bfs, ...
- BZOJ 1193 [HNOI2006]马步距离:大范围贪心 小范围暴搜
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1193 题意: 给定起点(px,py).终点(sx,sy).(x,y < 100000 ...
- [BZOJ1193][HNOI2006]马步距离(贪心+dfs)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1193 分析: 首先小范围可以直接暴力.(其实只要用上题目中的表就行了) 如果范围比较大 ...
- 【BZOJ 1193】 [HNOI2006]马步距离
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 原问题可以等价为两个点. 然后其中一个点要移动到另外一个点. 那么我们可以把左下角那个点(对称总是可以得到一个点在左下角)放在原点的 ...
随机推荐
- NFS挂载时出现"access denied by server while mounting"的解决方法
NFS挂载时出现"access denied by server while mounting"的解决方法 2015-01-14 何敏杰 3条评论 44,071次浏览 NFS是 ...
- Docker学习(三): Dockerfile指令介绍
特别声明: 博文主要是学习过程中的知识整理,以便之后的查阅回顾.部分内容来源于网络(如有摘录未标注请指出).内容如有差错,也欢迎指正! =============系列文章============= 1 ...
- CSS,js,html
图片盗链问题使用以下meta标签解决 <meta name="referrer" content="never"> Chrome 中文界面下默认会将 ...
- Access MetaData
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 使用javascript调用android代码
1.使用webview对象的addJavascriptInterface方法 2.addJavascriptInterface方法有两个参数,第一个参数就是我们一般会实现一个自己的类,类里面提供我们要 ...
- 【PyQt5 学习记录】003:水平布局和获取屏幕尺寸
#!/usr/bin/python3 # -*- coding:utf-8 -*- import sys from PySide2.QtWidgets import (QApplication, QW ...
- 常用SEO优化
- ArcGIS 地类净面积计算工具
地类净面积计算工具可以自己定义图层.字段.地类代码计算任意图层的椭球面积.线状地物扣除.零星扣除和其他扣除,计算地类净面积计算:可以用于二调数据图斑地类.规划地块和基本农田等等需要计算净面积的都可以. ...
- Mysql的命令
学习mysql命令必须先安装哦:安装教程 1.doc连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql ...
- centors7 elasticsearch6.3安装以及问题记录
1.安装elasticsearch . 安装系统:centors7 1.下载安装包 官网地址:https://www.elastic.co/downloads/past-releases 2.mac文 ...