传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4584
题意:找出曼哈顿距离最小的两个H,C点(H在前C在后),如果有多个相同,按x1,y1,x2,y2再选出最小的一个。
思路:总点数不超过1600,直接存储H和C的各点位置,排序,暴力。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
struct node
{
int x,y;
friend bool operator < (node a,node b){
if(a.x!=b.x) return a.x<b.x;
return a.y<b.y;
}
}H[1600],C[1600];
int m,n,h,c;
char ch;
int len(int i,int j)
{
return abs(H[i].x-C[j].x)+abs(H[i].y-C[j].y);
}
void solve()
{
int ans=1600,ansh,ansc;
sort(H,H+h);
sort(C,C+c);
for(int i=0;i<h;i++)
{
for(int j=0;j<c;j++)
{
int l=len(i,j);
if(ans>l)
{
ans=l;
ansh=i;
ansc=j;
}
}
}
printf("%d %d %d %d\n",H[ansh].x,H[ansh].y,C[ansc].x,C[ansc].y);
}
int main()
{
while(cin>>m>>n&&m&&n)
{
h=0;c=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>ch;
if(ch=='C') {C[c].y=j;C[c++].x=i;}
else if(ch=='H') {H[h].x=i;H[h++].y=j;}
}
}
solve();
}
return 0;
}