Nearest Neighbor Search 简单几何(求空间一点到区域的距离)

题目链接:
https://acm.bnu.edu.cn/v3/statments/52296.pdf

题意:
求空间一点(x0,y0,z0),到三维空间区域的最小距离?

分析:
先求二维平面的最小距离(将平面分为9部分分别处理即可),再求高的距离。
这里写图片描述

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;


int main()
{
    long long x0,x1,x2,y0,y1,y2,z0,z1,z2;
    while(scanf("%lld%lld%lld",&x0,&y0,&z0)!=EOF)
    {
        scanf("%lld%lld%lld",&x1,&y1,&z1);
        scanf("%lld%lld%lld",&x2,&y2,&z2);
        long long dis1, dis2=0;
        if(x0 >=x1 && x0 <= x2 && y0 >=y1 && y0 <= y2 &&  z0 >=z1 && z0 <= z2)
        {
            printf("0\n"); continue;
        }
        if(x0<x1 && y0<y1) dis1 = (x1-x0)*(x1-x0)+ (y1-y0)*(y1-y0);
        else if(x0>=x1 && x0<=x2 && y0 <= y1) dis1 = (y1-y0)*(y1-y0);
        else if(x0>x2 && y0<y1) dis1 = (x2-x0)*(x2-x0) + (y1-y0)*(y1-y0);
        else if(x0<x1 && y0>=y1 && y0<=y2) dis1 = (x1-x0)*(x1-x0);
        else if(x0>=x1 && x0<=x2 && y0>=y1 && y0<=y2) dis1 = 0;
        else if(x0>x2 && y0>=y1 && y0<=y2) dis1 = (x0-x2)*(x0-x2);
        else if(x0<x1 && y0>y2) dis1 = (x0-x1)*(x0-x1)+(y0-y2)*(y0-y2);
        else if(x0>=x1 && x0<=x2 && y0 >y2) dis1 = (y0-y2)*(y0-y2);
        else dis1 = (x0-x2)*(x0-x2)+(y0-y2)*(y0-y2);

        if(z0>=z1 && z0<=z2) dis2 = 0;
        else dis2 = min((z0-z1)*(z0-z1),(z0-z2)*(z0-z2));

        printf("%lld\n",dis1+dis2);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值