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> # ...
随机推荐
- 【php】关于trim,rtrim,ltrim,substr 的字符串切割导致 json,_encode无法 识别数据的问题
示例 <?php $a = rtrim('南宁 .',' .'); echo $a; //输出 南�� echo json_encode($a); //输出空白 $b = ['name'=> ...
- MySQL Limit 限定查询记录数
MySQL Limit 限定查询记录数 MySQL LIMIT MySQL 中 LIMIT 关键字用于限定查询记录返回最大数目. 语法: ... LIMIT offset , rows 该语法中,of ...
- Partitioning by Palindromes UVA - 11584 简单dp
题目:题目链接 思路:预处理出l到r为回文串的子串,然后如果j到i为回文串,dp[i] = min(dp[i], dp[j] + 1) AC代码: #include <iostream> ...
- POJ:1995-Raising Modulo Numbers(快速幂)
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9512 Accepted: 578 ...
- Jsoncpp 编译
1. linux下编译jsoncpp 从(http://jsoncpp.sourceforge.net/)下载源码包“jsoncpp-src-0.5.0.tar.gz”,解压后在其解压后目录中运行 $ ...
- javascript md5 二次加密 和 java md5 二次加密结果不同
最近研究httpclient post 时遇到了一个问题,很费解. js md5(str) 和 java md5(str),第一次md5 加密结果一样,(当时忽略了大小写问题,java 大写,js小 ...
- 找出Xcode没有使用的图片
#! /bin/sh PROJ=`find . -name '*.xib' -o -name '*.[mh]'` for png in ` find . -name '*.png'` do name ...
- Linux忘记root密码的解决办法
这里以centos6为例: 第一步:先将系统重新启动,在读秒的时候按下任意键就会出现如下图的菜单界面: 第二步:按下『e』就能够进入grub的编辑模式,如图: 第三步:将光标移动到kernel那一行, ...
- CCF-NOIP-2018 提高组(复赛) 模拟试题(一)
T1 帽子戏法 问题描述 小 Y 有一个\(n*n*n\)的"帽子立方体" ,即一个\(n\)层的立方体,每层的帽子都 可以排成\(n*n\)的矩阵. "帽子立方体&qu ...
- php 代码段执行时间
<?php //程序运行时间 $starttime = explode(' ',microtime()); echo microtime(); /*········以下是代码区······· ...