Gym-101606B_计算几何

https://cn.vjudge.net/problem/Gym-101606B

枚举一个连线,算距离

#include<bits/stdc++.h>
using namespace std;

#define ll long long

ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int N=100+10;

struct P{
    int x,y;
    P operator-(P b){
        return {x-b.x,y-b.y};
    }
    double len(){return hypot(x,y);}
}p[N];

double cross(P a,P b){
    return a.x*b.y-a.y*b.x;
}

double dist(P p,P a,P b){
    return cross(p-a,b-a)/(b-a).len();
}

int main(){
    //freopen("in.txt","r",stdin);
    int n=rd();
    for(int i=0;i<n;i++)
    p[i].x=rd(),p[i].y=rd();
    double ans=1e100;
    for(int i=0;i<n;i++)
    for(int j=0;j<i;j++){
      //  cout<<"----"<<endl;
        double a=1e100,b=1e-100;
        for(int k=0;k<n;k++){
            a=min(a,dist(p[k],p[i],p[j]));
            b=max(b,dist(p[k],p[i],p[j]));
          //  cout<<a<<" "<<b<<" "<<k<<endl;
        }
        ans=min(ans,b-a);
        //cout<<i<<" "<<j<<endl;
        //cout<<a<<" "<<b<<" "<<ans<<endl;
    }
    printf("%.6f",ans);
    //cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/ujn20161222/article/details/80149835