(POJ - 3301)Texas Trip
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5943 Accepted: 1839
Description
After a day trip with his friend Dick, Harry noticed a strange pattern of tiny holes in the door of his SUV. The local American Tire store sells fiberglass patching material only in square sheets. What is the smallest patch that Harry needs to fix his door?
Assume that the holes are points on the integer lattice in the plane. Your job is to find the area of the smallest square that will cover all the holes.
Input
The first line of input contains a single integer T expressed in decimal with no leading zeroes, denoting the number of test cases to follow. The subsequent lines of input describe the test cases.
Each test case begins with a single line, containing a single integer n expressed in decimal with no leading zeroes, the number of points to follow; each of the following n lines contains two integers x and y, both expressed in decimal with no leading zeroes, giving the coordinates of one of your points.
You are guaranteed that T ≤ 30 and that no data set contains more than 30 points. All points in each data set will be no more than 500 units away from (0,0).
Output
Print, on a single line with two decimal places of precision, the area of the smallest square containing all of your points.
Sample Input
2
4
-1 -1
1 -1
1 1
-1 1
4
10 1
10 -1
-10 1
-10 -1
Sample Output
4.00
242.00
题目大意:平面上有n个点,求一个面积最小的正方形,使得这个正方形可以覆盖所有点。
思路:首先要知道平面坐标旋转公式,已知一个点p(x,y),若坐标轴逆时针旋转θ角度,得到点p坐标为(x1,y1)。则
x1=xcosθ−ysinθ,y1=ycosθ+xsinθ
。对于坐标旋转的解释见下图:
知道坐标旋转公示后,我们自然而然会去想去三分角度,对于每一个旋转角度,都有一个x轴方向距离最远的两点
max△x
和y轴上距离最远的两点
max△y
,而正方形的边长就是max{
max△x,max△y
},随着旋转角度的增加,这个边长必然会有一个最小值,即当
max△x=max△y
时取到最小值,这个便是我们要求的答案。
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double pi=acos(-1.0);
const int maxn=35;
int n;
struct node
{
double x,y;
}a[maxn];
double check(double t)
{
double maxx=-INF,minx=INF,maxy=-INF,miny=INF;
for(int i=0;i<n;i++)
{
double tmpx=a[i].x*cos(t)-a[i].y*sin(t);
maxx=max(maxx,tmpx);
minx=min(minx,tmpx);
double tmpy=a[i].y*cos(t)+a[i].x*sin(t);
maxy=max(maxy,tmpy);
miny=min(miny,tmpy);
}
return max(maxx-minx,maxy-miny);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
double lo=0,hi=pi/2.0,mid,midmid;
while(hi-lo>eps)
{
mid=(hi+lo)/2.0;
midmid=(hi+mid)/2.0;
if(check(mid)<check(midmid)) hi=midmid;
else lo=mid;
}
double ans=check(lo)*check(lo);
printf("%.2f\n",ans);
}
return 0;
}