问题
https://vjudge.net/problem/UVA-1531
分析
设 六边形边长为L,那么六边形的重心坐标是
(
x
L
,
y
3
2
L
)
(xL,y\frac{\sqrt{3}}{2}L)
(xL,y23L),x,y要奇偶性相同。
首先求出两个点的所在六边形的中心坐标,然后计算两个六边形的距离。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn=100000+5;
const double eps=1e-6,sqrt3=sqrt(3.0);
int n,s,kase=0;
struct Center{
int x,y;
Center(int x=0,int y=0):x(x),y(y){}
}pa,pb;
double L,xa,ya,xb,yb;
int dx[]={0,1,1,-1,-1,0},dy[]={2,1,-1,1,-1,-2};
inline double distant(double t1,double t2,double t3,double t4){
return sqrt((t3-t1)*(t3-t1)+(t4-t2)*(t4-t2));
}
inline Center getCenter(double x,double y,double &disx){
int row=x/(1.5*L),col=y/(sqrt3*L/2);
if((row-col)%2!=0){
col++;
}
disx=distant(row*(1.5*L),col*(sqrt3*L/2),x,y);
double t=disx;
int fx=row,fy=col;
for(int i=0;i<6;++i){
int nx=row+dx[i],ny=col+dy[i];
t=distant(nx*(1.5*L),ny*(sqrt3*L/2),x,y);
if(t<disx-eps){
disx=t;
fx=nx;
fy=ny;
}
}
return Center(fx,fy);
}
inline Center getLine(){
Center t;
t.x=abs(pa.x-pb.x);
t.y=abs(pa.y-pb.y);
if(t.x>=t.y) return Center(t.x,0);
else return Center(t.x,(t.y-t.x)/2);
}
int main(void){
while(scanf("%lf%lf%lf%lf%lf",&L,&xa,&ya,&xb,&yb) && L>eps){
double disa=0,disb=0;
pa=getCenter(xa,ya,disa);
pb=getCenter(xb,yb,disb);
if(pa.x==pb.x && pa.y==pb.y){
printf("%.3lf\n",distant(xa,ya,xb,yb));
continue;
}
Center ans=getLine();
printf("%.3lf\n",disa+disb+(L*sqrt3)*ans.x+(sqrt3*L)*ans.y);
}
return 0;
}