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

题面

问题描述

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

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

输入样例

第一行 \(3\) 个整数为 \(N,X,Y\)
第二行 \(4\) 个整数为 \(x_1,y_1,x_2,y_2\)
下面将有 \(N\) 行,为 \(N\) 个敌人所在的坐标

2 5 6
0 0 4 0
2 1
2 3

输出样例

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

2 14

题解

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

Step 1

先跑一遍 \(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 \geq r\) 的情况可能不符合正确性。如果直接输出最后一次的答案可能会错。
还有在 \(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;
}
posted @ 2021-05-29 19:56  init-神眷の樱花  阅读(66)  评论(0)    收藏  举报