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> # ...
随机推荐
- Python常用函数记录
Python常用函数/方法记录 一. Python的random模块: 导入模块: import random 1. random()方法: 如上如可知该函数返回一个[0,1)(左闭右开)的一个随机的 ...
- HyperLedger Fabric 1.4 区块链技术发展(1.3)
区块链技术发展经历区块链1.0(数字货币).区块链2.0(数字资产与智能合约)和区块链3.0(各种行业分布式应用落地)三个阶段.区块链在应用上分为公有链(PublicBlockChains).联盟链( ...
- POJ :3614-Sunscreen
传送门:http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- python向多个邮箱发邮件--注意接收是垃圾邮件
群发邮件注意:三处标红的地方 # -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText from emai ...
- python基础之布尔运算、集合
布尔值 True 真 False 假 所有的数据类型都自带布尔值,数据只有在0,None和空的时候为False. print(bool()) print(bool()) print(bool('')) ...
- TouTiao开源项目 分析笔记1
1.InitApp==>项目的入口Application 1.1.继承了MultiDexApplication 超过65K方法的APP,会遇到65535的错误.原因就是为了支持比较大型的APP而 ...
- android onNewIntent 为什么要在onNewIntent的时候要显示的去调用setIntent
原因: 当调用到onNewIntent(intent)的时候,需要在onNewIntent() 中使用setIntent(intent)赋值给Activity的Intent.否则,后续的getInte ...
- Asp.net Core发布到CentOS7
第一步.安装CentOS 官网https://www.centos.org/下载CentOS,下载地址https://www.centos.org/download/,我选的“DVD ISO”,然后虚 ...
- Android保持屏幕常亮唤醒状态
第一步: 首先添加权限: <uses-permission android:name="android.permission.WAKE_LOCK"></uses ...
- atomic integer 实现
public final int getAndAddInt(Object o, long offset, int delta) { int v; do { v = getIntVolatile(o, ...