POJ 1444 计算几何趣题

时间:2023-02-10 20:35:29

求长方体上两点表面距离。

用到了比较神奇的递归,很有趣。学习之。

将1点旋转至XOY平面,再用递归四面展开,限制步数,得到最近的距离。

/*********************
* Creater:Sevenster *
* Time:2012.07.31 13:28 *
* PID:POJ 1444 *
*********************/
#include<iostream>
#include<cmath>
using namespace std;

double length2( double x1, double y1, double x2, double y2 )
{
return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
}

double ans;
void getAns( int i, int j, double x, double y, double x2, double y2, double z2, double l, double w, double h )
{
if( z2==0 && ans>length2( x, y, x2, y2 ) )
ans=length2( x, y, x2, y2 );
//else
{
if( i>= 0 && i< 2 )
getAns( i+ 1, j, x+ h, y, h- z2, y2, x2, h, w, l);
if( i<= 0 && i> -2 )
getAns( i- 1, j, x- l, y, z2, y2, l- x2, h, w, l);
if( j>= 0 && j< 2 )
getAns( i, j+ 1, x, y- w, x2, z2, w- y2, l, h, w);
if( j<= 0 && j> -2 )
getAns( i, j- 1, x, y+ h, x2, h- z2, y2, l, h, w);
}
}

int main()
{
double l, w, h;
double INF = 1e20;
double x1, y1, z1;
double x2, y2, z2;

while( scanf( "%lf%lf%lf",&l, &w, &h )!= EOF )
{
scanf( "%lf%lf%lf", &x1, &y1, &z1 );
scanf( "%lf%lf%lf", &x2, &y2, &z2 );
if( z1!=0 && z1!=h )
{
if( x1!=0 && x1!=l )
{
swap( y1,z1 );
swap( y2,z2 );
swap( w,h );
}
else
{
swap( x1,z1 );
swap( x2,z2 );
swap( l,h );
}
}
if( z1==h )
{
z1=0;
z2=h-z2;
}
ans=INF;
getAns(0, 0, x1, y1, x2, y2, z2, l, w, h );
printf("%.0lf\n",ans);
}
return 0;
}