44
这一题很水的,不像难度四的,两个点之间的长度在本题中为d=abs(x1-x2)+abs(y1-y2);枚举可能的所有邮局的点,每个点的对应最小值可以算出来,取最小的就好了
AC代码:
# include <stdio.h> # include <algorithm> # include <math.h> # include <stdlib.h> using namespace std; const int maxn=2000000000; struct node{ int c, r; }; struct node s[100]; int main(){ int t, n, i, j, k, l; scanf("%d", &t); for(i=1; i<=t; i++){ scanf("%d", &n); int min_r=maxn, max_c=-1, max_r=-1, min_c=maxn; for(j=1; j<=n; j++){ scanf("%d%d", &s[j].r, &s[j].c); min_r=min(min_r, s[j].r); max_r=max(max_r, s[j].r); min_c=min(min_c, s[j].c); max_c=max(max_c, s[j].c); } int Min=maxn; for(j=min_r; j<=max_r; j++){ for(k=min_c; k<=max_c; k++){ int sum=0; for(l=1; l<=n; l++){ sum=sum+abs(j-s[l].r)+abs(k-s[l].c); } Min=min(Min, sum); } } printf("%d\n", Min); } return 0; }