题目链接:
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;
}