poj3301 Texas Trip(坐标旋转+三分)

本文介绍了一种解决TexasTrip问题的方法,通过旋转点集并寻找最小覆盖正方形来修复车门上的孔洞。该算法利用三分法在不同角度下找到最优解,最终输出所需的正方形面积。

Texas Trip

http://poj.org/problem?id=3301

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4709 Accepted: 1473

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

将所有点绕原点旋转,然后求边平行于坐标轴的覆盖所有点的正方形的面积 ,然后再对角度进行三分,找出面积最小时的角度。

在这其中有两个需要注意的问题:

1、点旋转的公式:X‘=X*cos(a)-Y*sin(a);Y'=Y*cos(a)+X*sin(a) 

2、为什么要使用三分?

     答:可以发现面积和角度之间的函数关系并不明确,也没有可以约束出最小值的条件,因此选用三分来直接对区间中的值来进行对比。简单说就是:不能二分了,只好三分。

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>

#define md(x,y) (x+y)/2
const double eps=1e-10;
const double pi=acos(-1.0);
using namespace std;
struct node{
	double x,y;
};
int dim_cmp(double a,double b)
{
	if(fabs(a-b)<eps) return 0;
	if(a-b<0) return -1;
	return 1; 
}
node date[35];int n;
inline node rot(node a,double angle)
{
	node temp;
	temp.x=a.x*cos(angle)-a.y*sin(angle);
	temp.y=a.y*cos(angle)+a.x*sin(angle);
	return temp;
}	
double get_s(double angle)
{
	double maxx=-505,maxy=-505,minx=505,miny=505;
	for(int i=0;i<n;i++)
	{
		node Aft=rot(date[i],angle);
		maxx=max(Aft.x,maxx);
		maxy=max(Aft.y,maxy);
		minx=min(Aft.x,minx);
		miny=min(Aft.y,miny);
	}
	double fin_edg=max((maxx-minx),(maxy-miny));
	return fin_edg*fin_edg;
}
int main()
{
	double re;
	int t;
	cin>>re;
	t=re;
	while(t--)
	{
		
		cin>>re;
		n=re;
		int i;
		for(i=0;i<n;i++)
		{
			cin>>date[i].x>>date[i].y;
		}
		double l=0,r=pi,midl,midr;
		double ans;
		while(dim_cmp(l,r)<0)
		{
			midl=md(l,r);
			midr=md(midl,r);
			double s1=get_s(midl),s2=get_s(midr);
			if(dim_cmp(s1,s2)<=0)
			{
				ans=s1;
				r=midr;
			}
			else
			{
				ans=s2;
				l=midl;
			}
			//printf("%lf\n",l-r);
		}
		printf("%.2f\n",ans);
	}
	return 0;
}

注意,最后的输出中如果选用G++则应用&f如选用C++则无此限制

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值