【机房模拟赛 5.5】escape 大逃亡

题面

问题描述

给出数字 N ( 1 ≤ N ≤ 10000 ) , X ( 1 ≤ X ≤ 1000 ) , Y ( 1 ≤ Y ≤ 1000 N(1 \leq N \leq 10000),X(1 \leq X \leq 1000),Y(1 \leq Y \leq 1000 N(1N10000),X(1X1000),Y(1Y1000)代表有 N N N 个敌人分布在一个 X X X Y Y Y 列的矩阵上,矩形的行号从 0 0 0 X − 1 X-1 X1,列号从 0 0 0 Y − 1 Y-1 Y1。再给出四个数字 x 1 , y 1 , x 2 , y 2 x_1,y_1,x_2,y_2 x1,y1,x2,y2 分别代表你要从起点 ( x 1 , y 1 ) (x_1,y_1) (x1,y1) 移动到目标点 ( x 2 , y 2 ) (x_2,y_2) (x2,y2)。在移动的过程中你当然希望离敌人的距离的最小值最大化,现在请求出这个值最大可以为多少?以及在这个前提下,你最少要走多少步才可以到目标点。

注意这里距离的定义为两点的曼哈顿距离,即某两个点的坐标分为 ( a , b ) , ( c , d ) (a,b),(c,d) (a,b),(c,d),那么它们的距离为 ∣ a − c ∣ + ∣ b − d ∣ |a-c|+|b-d| ac+bd

输入样例

第一行 3 3 3 个整数为 N , X , Y N,X,Y N,X,Y
第二行 4 4 4 个整数为 x 1 , y 1 , x 2 , y 2 x_1,y_1,x_2,y_2 x1,y1,x2,y2
下面将有 N N N 行,为 N N N 个敌人所在的坐标

2 5 6
0 0 4 0
2 1
2 3

输出样例

在一行内输出你离敌人的距离及在这个距离的限制下,你到目标点最少要移动多少步。

2 14

题解

毒瘤题,正解竟然是爆搜。。。
看到最小值最大化,想到二分离敌人的最小距离。于是我们还差一个判断合法性的程序,想到搜图常用的 B F S BFS BFS,算一下复杂度,设长和宽是 O ( n ) O(n) O(n) 级别的,每次跑满是 O ( n 2 ) O(n ^2) O(n2) 的,也就是 1 e 6 1e6 1e6,二分的边界是 0 0 0 ~ 2 n − 2 2n - 2 2n2,也就是 O ( l o g n ) O(logn) O(logn) 的,总复杂度也就是 O ( n 2 l o g n ) O(n^2logn) O(n2logn) ,而且还跑不满,考试的时候竟然没去算就放弃了。还以为要打 D P DP DP, 唉。

Step 1

先跑一遍 B F S BFS BFS,预处理出矩阵中每个点离它最近的敌人的曼哈顿距离。

void init() {
	memset(d,0x3f,sizeof(d));
	for(re int i = 1; i <= tot; i++) {
		q.push((node){a[i].x,a[i].y});
		vis[a[i].x][a[i].y] = true;
		d[a[i].x][a[i].y] = 0;
	}
	while(!q.empty()) {
		node now = q.front(); q.pop();
		for(re int i = 0; i <= 3; i++) {
			int tox = now.x + xx[i];
			int toy = now.y + yy[i];
			if(tox < 0 || tox > n - 1 || toy < 0 || toy > m - 1) continue;
			d[tox][toy] = min(d[tox][toy],d[now.x][now.y] + 1);
			if(!vis[tox][toy]) q.push((node){tox,toy}),vis[tox][toy] = true;
		}
	}
}

Step 2

二分答案,但有一个要注意的地方。
要记录一下二分时的两个答案,因为最后一次使 l ≥ r l \geq r lr 的情况可能不符合正确性。如果直接输出最后一次的答案可能会错。
还有在 B F S BFS BFS 的时候不要忘记判断一下起始点是否的曼哈顿距离是否符合条件,最后还要判断能否到达终点。

bool judge(int len) {
	memset(dis,0x3f,sizeof(dis));
	memset(vis,false,sizeof(vis));
	while(!q.empty()) q.pop();
	q.push((node){sx,sy}); vis[sx][sy] = true;
	if(d[sx][sy] < len || d[tx][ty] < len) return false;
	dis[sx][sy] = 0;
	while(!q.empty()) {
		node now = q.front(); q.pop();
		for(re int i = 0; i <= 3; i++) {
			re int tox = now.x + xx[i];
			re int toy = now.y + yy[i];
			if(tox < 0 || tox > n - 1 || toy < 0 || toy > m - 1 || d[tox][toy] < len) continue;
			if(!vis[tox][toy]) q.push((node){tox,toy}),vis[tox][toy] = true;
			dis[tox][toy] = min(dis[tox][toy],dis[now.x][now.y] + 1);
			if(tox == tx && toy == ty) return true;
		}
	}
	return dis[tx][ty] != 1061109567;
}

完整代码

#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#define re register
using namespace std;
const int N = 1e5 + 5;
const int M = 1e3 + 5;
struct node {
	int x,y;
}a[N];
int d[M][M],dis[M][M],tot,n,m,sx,sy,tx,ty;
int xx[4] = {0,0,1,-1};
int yy[4] = {1,-1,0,0};
inline int read() {
	re int x = 0,flag = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9'){if(ch == '-')flag = -1;ch = getchar();}
	while(ch >='0' && ch <='9'){x = (x << 3) + (x << 1) + ch - 48;ch = getchar();}
	return x * flag;
}
std::queue<node> q;
bool vis[M][M];
void init() {
	memset(d,0x3f,sizeof(d));
	for(re int i = 1; i <= tot; i++) {
		q.push((node){a[i].x,a[i].y});
		vis[a[i].x][a[i].y] = true;
		d[a[i].x][a[i].y] = 0;
	}
	while(!q.empty()) {
		node now = q.front(); q.pop();
		for(re int i = 0; i <= 3; i++) {
			int tox = now.x + xx[i];
			int toy = now.y + yy[i];
			if(tox < 0 || tox > n - 1 || toy < 0 || toy > m - 1) continue;
			d[tox][toy] = min(d[tox][toy],d[now.x][now.y] + 1);
			if(!vis[tox][toy]) q.push((node){tox,toy}),vis[tox][toy] = true;
		}
	}
}
bool judge(int len) {
	memset(dis,0x3f,sizeof(dis));
	memset(vis,false,sizeof(vis));
	while(!q.empty()) q.pop();
	q.push((node){sx,sy}); vis[sx][sy] = true;
	if(d[sx][sy] < len || d[tx][ty] < len) return false;
	dis[sx][sy] = 0;
	while(!q.empty()) {
		node now = q.front(); q.pop();
		for(re int i = 0; i <= 3; i++) {
			re int tox = now.x + xx[i];
			re int toy = now.y + yy[i];
			if(tox < 0 || tox > n - 1 || toy < 0 || toy > m - 1 || d[tox][toy] < len) continue;
			if(!vis[tox][toy]) q.push((node){tox,toy}),vis[tox][toy] = true;
			dis[tox][toy] = min(dis[tox][toy],dis[now.x][now.y] + 1);
			if(tox == tx && toy == ty) return true;
		}
	}
	return dis[tx][ty] != 1061109567;
}
int main() {
	freopen("escape.in","r",stdin);
	freopen("escape.out","w",stdout);
	tot = read(),n = read(),m = read();
	sx = read(),sy = read(),tx = read(),ty = read();
	for(re int i = 1; i <= tot; i++)
		a[i].x = read(),a[i].y = read();
	init();
	int l = 0,r = n + m - 2,ans,res;
	while(l <= r) {
		re int mid = (l + r) >> 1;
		if(!judge(mid)) r = mid - 1;
		else res = mid, l = mid + 1,ans = dis[tx][ty];
	}
	printf("%d %d\n",res,ans);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值