Smallest Bounding Rectangle - uva10173
Smallest Bounding Rectangle
Given the Cartesian coordinates of n(>0)2-dimensional points, write a program that computes the area of their smallest bounding rectangle (smallest rectangle containing all the given points).
Input
The input le may contain multiple test cases. Each test case begins with a line containing a positive
integer n(<1001) indicating the number of points in this test case. Then follows n lines each containing
two real numbers giving respectively the x - and y
-coordinates of a point. The input terminates with a
test case containing a value 0 for n which must not be processed.
Output
For each test case in the input print a line containing the area of the smallest bounding rectangle
rounded to the 4th digit after the decimal point.
Sample Input
3
-3.000 5.000
7.000 9.000
17.000 5.000
4
10.000 10.000
10.000 20.000
20.000 20.000
20.000 10.000
0
Sample Output
80.0000
100.0000
最小外接矩形,不会简单的方法,只能将所给点构成凸包,然后枚举凸包上相邻的两点与x轴的夹角作为矩形的倾斜角c,然后求出沿倾斜角c的方向上的凸包映射的长度和垂直倾斜角c的方向上的映射长度为矩形的长和宽.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int Max = 1011;
const double eps = 1e-8;
const double Pi = acos(-1.0);
int sgn(double x)//精度处理
{
if(fabs(x)<eps)
{
return 0;
}
if(x<0)
{
return -1;
}
else
{
return 1;
}
}
struct Point
{
double x,y;
Point() {};
Point(double _x,double _y)
{
x=_x;
y=_y;
}
Point operator - (const Point &b)const
{
return Point(x-b.x,y-b.y);
}
double operator ^ (const Point &b)const
{
return x*b.y-y*b.x;
}
double operator * (const Point &b)const
{
return x*b.x+y*b.y;
}
void transXY(double B)
{
double tx = x,ty=y;
x =tx*cos(B)-ty*sin(B);
y = tx*sin(B)+ty*cos(B);
}
} List[Max];
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
int Stack[Max],top;
bool _cmp(Point p1,Point p2)//极角排序
{
double tmp = (p1-List[0])^(p2-List[0]);
if(sgn(tmp)>0)
{
return true;
}
else if(sgn(tmp)==0 && sgn(dist(p1,List[0])-dist(p2,List[0]))<=0)
{
return true;
}
else
{
return false;
}
}
void Graham(int n)//凸包
{
Point p0;
int k = 0;
p0=List[0];
for(int i=1; i<n; i++)
{
if((p0.y>List[i].y)||(p0.y==List[i].y&&p0.x>List[i].x))
{
p0=List[i];
k=i;
}
}
swap(List[k],List[0]);
sort(List+1,List+n,_cmp);
if(n==1)
{
top=1;
Stack[0]=0;
return ;
}
if(n==2)
{
top= 2 ;
Stack[0]=0;
Stack[1]=1;
return ;
}
Stack[0]= 0 ;
Stack[1]=1;
top=2;
for(int i=2; i<n; i++)
{
while(top>1&&sgn((List[Stack[top-1]]-List[Stack[top-2]])^(List[i]-List[Stack[top-2]]))<=0)
top--;
Stack[top++]=i;
}
}
double Get(int n)//计算矩形的最小面积
{
double ant,ans ,dis ;
double x,y;
double sum = INF;
Point a;
for(int i=0; i<n; i++)
{
a=List[Stack[(i+1)%n]]-List[Stack[i]];
ant = atan2(a.y,a.x);//倾斜角
x = 0;
y = 0;
for(int j=0; j<n; j++)
{
a=List[Stack[(j+1)%n]]-List[Stack[j]];
dis =sqrt(a.x*a.x+a.y*a.y);
ans = atan2(a.y,a.x);
x+=fabs(dis*sin(ans-ant));//映射总和
y+=fabs(dis*cos(ans-ant));//映射总和
}
sum = min(x*y,sum);
}
return sum/4;
}
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
top = 0;
for(int i=0; i<n; i++)
{
scanf("%lf %lf",&List[i].x,&List[i].y);
}
Graham(n);
printf("%.4f\n",Get(top));
}
return 0;
}
Smallest Bounding Rectangle - uva10173的更多相关文章
- 此坑待填 离散化思想和凸包 UVA - 10173 Smallest Bounding Rectangle
Smallest Bounding Rectangle Given the Cartesian coordinates of n(>0)2-dimensional points, write a ...
- UVA10173 Smallest Bounding Rectangle 最小面积矩形覆盖
\(\color{#0066ff}{题目描述}\) 给定n(>0)二维点的笛卡尔坐标,编写一个程序,计算其最小边界矩形的面积(包含所有给定点的最小矩形). 输入文件可以包含多个测试样例.每个测试 ...
- UVA 12307 Smallest Enclosing Rectangle
https://vjudge.net/problem/UVA-12307 求覆盖所有点的最小矩形面积.周长 相当于求凸包的最小面积外接矩形.最小周长外接矩形 结论: 这个矩形一定有一条边和凸包上一条边 ...
- UVA 12307 Smallest Enclosing Rectangle(旋转卡壳)
题意:给你一些点,找出两个可以包含所有点的矩形,一个保证矩形面积最小,一个保证矩形周长最小,输出两个最小值 题解:首先根据所有点求一个凸包,再在这个凸包上枚举每条边,作为矩形的一条边(这样可以保证最小 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- Smallest Rectangle Enclosing Black Pixels
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- LeetCode Smallest Rectangle Enclosing Black Pixels
原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...
- 302. Smallest Rectangle Enclosing Black Pixels
题目: An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The b ...
随机推荐
- MarkMan – 马克鳗,让设计更有爱!
scavin(Google+) on 2010.11.16. MarkMan – 马克鳗 是一款方便高效的标注工具,极大节省设计师在设计稿上添加和修改标注的时间,让设计更有爱.Adobe AIR 平台 ...
- Git分布式项目管理
Git简介 Git是什么? Git和SVN一样都是一种高效的管理代码的系统. Git是目前世界上最先进的分布式版本控制系统(没有之一). 创建版本库 什么是版本库呢?版本库又名仓库,英文名 ...
- Oracle加密表空间进行数据加密的示例
接上篇:http://www.cnblogs.com/myrunning/p/4292049.html 1查看数据库版本 2查看当前数据库表空间 从这里看到我们此时数据库里没有加密表空间. 3创建加密 ...
- 除了白名单外的IP每秒最多处理 8 个请求 limit_conn_zone
防止黑客知道你的源服务器真实IP进行并发攻击,通常只需要保护动态文件请求,�php. 添加文件 nginx/conf/limit/whiteip.conf 里面是你要忽略限制的 白名单 IP地址,通常 ...
- 深入理解C++虚函数表
虚函数表是C++类中存放虚函数的一张表,理解虚函数表对于理解多态很重要. 本次使用的编译器是VS2013,为了简化操作,不用去操作函数指针,我使用到了VS的CL编译选项来查看类的内存布局. CL使用方 ...
- 杭电ACM 1197
#include<stdio.h>main(){ int temp,i,t,sum10,sum12,sum16; for(i=1000;i<=9999;i++) { temp=i; ...
- linq判断集合是否为空的方法
Enumerable.Any 扩展方法可以判断集合为空: 如果不为空 if (!source.Any()) { //... }
- 最长公共子序列PK最长公共子串
1.先科普下最长公共子序列 & 最长公共子串的区别: 找两个字符串的最长公共子串,这个子串要求在原字符串中是连续的.而最长公共子序列则并不要求连续. (1)递归方法求最长公共子序列的长度 1) ...
- dubbo源码分析2-reference bean发起服务方法调用
dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...
- .net下的跨域问题
环境: IIS7.0 MVC 4.0 公司官网 asp.net 需要的报名系统,需要有后台管理 由于是配合传统产业,所以MVC系统的数据,是由AIPS系统提供. (制作前是考虑去年用 ...