题目大意
有M*M网格,左下角(0,0),右上角(M-1,M-1).上面有n个餐厅,其中编号为1和2的分别是AB两宾馆的餐厅.对于一个位置,若存在与之前已有的任意餐厅位置相比,更靠近A或者B,就定义为”好位置”.问”好位置”的个数.(其中,两点间的距离为曼哈顿距离,即横纵坐标差的绝对值之和)
分析
主要是采用扫描线方法,在这里其实有一点不是很懂,为什么A和B的正上下不是好位置,不解```
#include<iostream>
#include<algorithm>
#include<string.h>
#define maxm 60000+10
#define Inf 0x3f3f3f3f
using namespace std;
static auto _ = []() //加快输入输出的速度 ,当有scanrf 和printf 时混用会出错
{
ios::sync_with_stdio(false);
cin.tie(0);
return 0;
}();
int d[maxm];
int n,m;
int main() {
int t;
cin >> t;
while (t--) {
int x1, y1, x2, y2,x,y;
cin >> m >> n;
cin >> x1 >> y1 >> x2 >> y2;
if (x1 > x2) swap(x1, x2);
for (int i = x1; i <=x2; i++)
d[i] = m;
n -= 2;
for (int i = 0; i < n; i++) {
cin >> x >> y;
d[x] = min(d[x], abs(y - y1));
}
d[x1] = d[x2] = 0;
for (int i = x1 + 1; i < x2; i++) {
d[i] = min(d[i - 1] + 1, d[i]);
}
for (int i = x2-1; i>x1; i--) {
d[i] = min(d[i+1] + 1, d[i]);
}
long long ans = 0;
for (int i = x1 + 1; i < x2; i++){
if (d[i]) {
ans++;
ans+=min(d[i]-1,m-1-y1);
ans+= min(d[i] - 1, y1);
}
}
cout << ans << endl;
}
return 0;
}