跳到正文

题解

P1332 血色先锋队 题解

duminghao123约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年7月2日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 1 分钟
题目
P1332

版本

  1. v2当前
  2. v1

P1332 血色先锋队 题解

Tips: 不用 DFS 也能通过

题目传送门

我们可以枚举领主到感染源的曼哈顿距离,求最小值即可。

何为曼哈顿距离?

假设坐标系中两个坐标分别为 (x1,y1)(x_1,y_1),(x2,y2)(x_2,y_2)。

曼哈顿距离就是 ∣x1−x2∣+∣y1−y2∣|x_1-x_2|+|y_1-y_2| 的值。

上代码

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,a,b,mn;
struct pos{
	int x,y;
}yt[100001],lz;
int main(){
	cin>>n>>m>>a>>b;
	for(int i=1;i<=a;i++) cin>>yt[i].x>>yt[i].y;
	for(int i=1;i<=b;i++){
		cin>>lz.x>>lz.y;
		mn=2147483647;
		for(int j=1;j<=a;j++){
			int l=max(yt[j].x-lz.x,lz.x-yt[j].x)+max(yt[j].y-lz.y,lz.y-yt[j].y);
			if(l<mn) mn=l;
		}
		cout<<mn<<endl;
	}
	return 0;
}
原文