P1332 血色先锋队 题解

· · 题解

P1332 血色先锋队 题解

Tips: 不用 DFS 也能通过

题目传送门

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

::::info[何为曼哈顿距离?] 假设坐标系中两个坐标分别为 (x_1,y_1),(x_2,y_2)。

曼哈顿距离就是 |x_1-x_2|+|y_1-y_2| 的值。 ::::

上代码


#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;
}