UVA 10652 Board Wrapping(二维凸包)
刘汝佳《算法竞赛入门经典》P272例题6包装木板
题意:有n块矩形木板,你的任务是用一个面积尽量小的凸多边形把它们抱起来,并计算出木板占整个包装面积的百分比。
输入:t组数据,每组先输入木板个数n,接下来n行,每行x,y,w,h,j。(x,y)是木板中心的坐标,w是宽,h是高,j是顺时针旋转的角度。
木板互不相交。
输出:对于每组数据,输出木板总面积占包装总面积的百分比,保留小数点后1位。
题解:典型的二维凸包问题,理解并调用模板即可。
附上凸包的证明过程,只看网址里的图,注意sort是极角排序:传送门。
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
typedef struct Point{
double x,y;
Point(double x=,double y=):x(x),y(y){}
}Vector;
Vector operator + (Vector A,Vector B)
{
return Vector(A.x+B.x,A.y+B.y);
}
Vector operator - (Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
bool operator <(const Point &a,const Point &b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
Vector Rotate(Vector A,double rad) //向量旋转
{
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
double ConvexPolygonArea(Point *p,int n)
{
double area=;
for(int i=;i<n-;i++)
area+=Cross(p[i]-p[],p[i+]-p[]);
return area/;
}
double torad(double deg)
{
return deg/*acos(-);
}
int ConvexHull(Point *p,int n,Point* ch) //注意是*ch
{
sort(p,p+n);
//不知道这里为什么不能加unique函数
//n=unique(p,p+n)-p;
int m=;
for(int i=;i<n;i++)
{
while(m>&&Cross(ch[m-]-ch[m-],p[i]-ch[m-])<=) m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-;i>=;i--) //注意是--不是++
{
while(m>k&&Cross(ch[m-]-ch[m-],p[i]-ch[m-])<=) m--;
ch[m++]=p[i];
}
if(n>) m--;
return m;//别忘了加m
}
int main()
{
int T;
Point P[],ch[];
scanf("%d",&T);
while(T--)
{
int n,pc=;
double area1=;
scanf("%d",&n);
for(int i=;i<n;i++)
{
double x,y,w,h,j,ang;
scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&j);
Point o(x,y);
ang=-torad(j);
P[pc++]=o+Rotate(Vector(-w/,-h/),ang);
P[pc++]=o+Rotate(Vector(w/,-h/),ang);
P[pc++]=o+Rotate(Vector(-w/,h/),ang);
P[pc++]=o+Rotate(Vector(w/,h/),ang);
area1+=w*h; //矩形总面积
}
int m=ConvexHull(P,pc,ch);
double area2=ConvexPolygonArea(ch,m); //凸包面积
//%被看做是格式说明符 所以要输出两个百分号
printf("%.1lf %%\n",area1*/area2);
}
return ;
}
UVA 10652 Board Wrapping(二维凸包)的更多相关文章
- uva 10652 Board Wrapping (计算几何-凸包)
Problem B Board Wrapping Input: standard input Output: standard output Time Limit: 2 seconds The sma ...
- Uva 10652 Board Wrapping(计算几何之凸包+点旋转)
题目大意:给出平面上许多矩形的中心点和倾斜角度,计算这些矩形面积占这个矩形点形成的最大凸包的面积比. 算法:GRAHAM,ANDREW. 题目非常的简单,就是裸的凸包 + 点旋转.这题自己不会的地方就 ...
- UVA 10652 Board Wrapping 计算几何
多边形凸包.. .. Board Wrapping Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu ...
- UVA 10652 Board Wrapping(凸包)
The small sawmill in Mission, British Columbia, hasdeveloped a brand new way of packaging boards for ...
- 简单几何(向量旋转+凸包+多边形面积) UVA 10652 Board Wrapping
题目传送门 题意:告诉若干个矩形的信息,问他们在凸多边形中所占的面积比例 分析:训练指南P272,矩形面积长*宽,只要计算出所有的点,用凸包后再求多边形面积.已知矩形的中心,向量在原点参考点再旋转,角 ...
- UVA 10652 Board Wrapping(凸包)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32286 [思路] 凸包 根据角度与中心点求出长方形所有点来,然后就 ...
- uva 10652 Board Wrapping
主要是凸包的应用: #include <cstdio> #include <cmath> #include <cstring> #include <algor ...
- ●UVA 10652 Board Wrapping
题链: https://vjudge.net/problem/UVA-10652 题解: 计算几何,Andrew求凸包, 裸题...(数组开小了,还整了半天...) 代码: #include<c ...
- 使用Graham扫描法求二维凸包的一个程序
#include <iostream> #include <cstring> #include <cstdlib> #include <cmath> # ...
随机推荐
- 多通道CNN
在读Convolutional Neural Networks for Sentence Classification 这个文章的时候,它在论文中提出一种模型变种就是 CNN-multichannel ...
- http虚拟主机的简单配置训练
http的虚拟主机 对于某些web访问站点而言,每天的访问量很少,因此真正的放一台服务器去进行web站点是很 浪费资源的,因此我们选择了虚拟主机 web处理模块的分类(MPM) 1.perfork 一 ...
- qt5.10.1编译记录
qt版本更新比较快,不知道选哪个版本合适,故选择一个较新版本的. 平台imx6 + linux4.1.16 + qt5.10.1 采用明远智睿提供的编译器:fsl-imx-fb-g ...
- Spring---单例模式(Singleton)的6种实现
1.1.1 摘要 在我们日常的工作中经常需要在应用程序中保持一个唯一的实例,如:IO处理,数据库操作等,由于这些对象都要占用重要的系统资源,所以我们必须限制这些实例的创建或始终使用一个公用的实例,这就 ...
- 2 js的20/80关键知识
1. 2 var a = 1; undefined a 1 alert(a); undefined var b = true; var c = "Hi"; undefined al ...
- android gradle 给所有的buildFlavor 的versionName 增加一个后缀
build里面有很多的productFlavors,我想要给所有的productFlavors 的versionName增加一个后缀比如:_20180323 怎么做?注意是所有的productFlav ...
- AS3项目基础框架搭建分享robotlegs2 + starling1.3 + feathers1.1
这个框架和我之前使用robotlegs1版本的大体相同,今天要写一个新的聊天软件就把之前的框架升级到了2.0并且把代码整理了一下. 使用适配器模式使得starling的DisplayObject和fl ...
- 关于python中的 if __name__ == 'main'
name 是内置变量,它表示的是当前所在模块的名字,同时还能反应一个包的结构. a ├── b │ ├── c.py │ └── __init__.py └── __init__.py 目录中 ...
- inspect流程
当node节点state为manage时,可执行inspector ironic node-set-provision-state <node_uuid> manage ironic no ...
- rownum浅谈(一)
只要做web开发,几乎没有不需要分页查询的,在oracle中,rownum就是用来进行处理分页的. 1.rownum是oracle对结果集返回的一个伪列,也就是说是先查询完结果之后再加上的一个虚列,相 ...