81-POJ-Wall(计算几何)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 41143 | Accepted: 14068 |
Description
Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements.
The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.
Input
Next N lines describe coordinates of castle's vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.
Output
Sample Input
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
Sample Output
1628
Hint
Source
证明:
先从简单的例子看起:假设现在的凸包有四个顶点构成,可以就一个顶点来观察,我们可以看到此处的周角由四个部分组成:2个直角,一个凸包内角,一个圆弧对应的圆心角。
同理每个顶点都有类似的关系,同时周角固定为360度,而凸包内角和为(4-2)*180 ;
所以总的圆弧对应的圆心角 = 4个周角 - 4 * 2个直角 - 4个凸包内角 = 4 * 360 - 4 * 2 * 90 - (4-2)*180 = 1440-720-360 =360度。
现在推广到n个顶点的凸包:
则所有内角和 = 周角 * n - n * 2个直角 - (n-2)*180 = 360*n - n *180 - n*180 + 360 = 360度。
故对于任意的凸包,所有的圆弧对应的圆心角之和都为360度,它们构成一个完整的圆。
ac代码如下:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define N 10005
const double PI = acos(-1.0); int n,tot;//n为二维平面上点的个数,tot为凸包上点的个数
struct node {
int x,y;
}a[N],p[N]; //p[]用来储存凸包 double dis(node a1,node b1){ //两点间距离公式
return sqrt((a1.x-b1.x)*(a1.x-b1.x)+(a1.y-b1.y)*(a1.y-b1.y) + 0.00);
} //叉积:返回结果为正说明p2在向量p0p1的左边(三点构成逆时针方向);
//为负则相反;为0则三点共线(叉积的性质很重要)
double multi(node p0,node p1,node p2){ //
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
} //极角排序:极角排序是根据坐标系内每一个点与x轴所成的角,逆时针比较。按照角度从小到大的方式排序
int cmp(node p1,node p2){ //极角排序;
int x=multi(p1,p2,a[0]);
if(x>0||(x==0&&dis(p1,a[0])<dis(p2,a[0])))
return 1;
return 0;
} void Graham(){ //求凸包
int k=0;
for(int i=0;i<n;i++) //找到最下最左的一个点
if(a[i].y<a[k].y||(a[i].y==a[k].y&&a[i].x<a[k].x))
k=i;
swap(a[0],a[k]); //将其设置为第一个点
sort(a+1,a+n,cmp);
tot=2,p[0]=a[0],p[1]=a[1]; //p[]模拟栈,用来储存凸包
for(int i=2;i<n;i++){
while(tot>1&&multi(p[tot-1],p[tot-2],a[i])>=0)
tot--; //右拐就回退
p[tot++]=a[i]; //左拐就放入
}
} double getArea(){
struct node b[3];
b[0] = p[0], b[1] = p[1], b[2] = p[2];
double area = 0;
for(int i = 2; i < tot; i++){
area += multi(b[0], b[1], p[i]) / 2.0;
b[1] = p[i];
}
return area;
} double getGirth(){
double rt = 0;
for(int i = 0; i < tot; i++){
rt += dis(p[i], p[(i+1)%tot]);
}
return rt;
} int main(){
double L;
while(cin >> n >> L){
tot = 0;
for(int i = 0; i < n; i++){
cin >> a[i].x >> a[i].y;
}
Graham();
double res = getGirth();
res += 2*PI*L;
cout << int(res + 0.5) << endl;
}
return 0;
}
最新文章
- Ehcache 缓存使用
- Moon.Orm 5.0其他额外配置的讲解
- SharePoint 2013 表单认证使用ASP.Net配置工具添加用户
- myBatis系列之四:关联数据的查询
- Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)
- JAVA GUI随笔
- [转]如何在 Visual Studio 中使用 Git 同步代码到 CodePlex
- javascript 传递引用类型参数
- Xhprof安装笔记(PHP性能监控)
- win8下光驱消失
- zabbix 通过smtp 邮件报警
- https配置
- Poj 2499 Binary Tree(贪心)
- php memcached+Mysql(主从)
- C#入门经典(2-重置窗体布局,界面介绍,错误列表)
- Android后台执行的定时器实现
- Redis 学习开发笔记
- 快速构建SPA框架SalutJS--项目工程目录 一
- JVM监控命令
- Java 三种方式实现接口校验
热门文章
- 通过修改注册表建立Windows自定义协议
- 安装sphinx报错(undefined reference to `libiconv_open&#39; 、undefined reference to `libiconv&#39;、undefined reference to `libiconv_close&#39;、make[1]: *** No rule to make target `all&#39;. Stop. 、make: *** [all-recursive
- 1-3 superset数据模型
- STM32学习笔记之__attribute__ ((at())绝对定位分析
- JAVA多线程三种实现方式
- (转)Inno Setup入门(三)——指定压缩方式
- Thread之六:线程创建方法
- 实现Runnable接口和继承Thread类
- 浅谈PHP面向对象编程(七、抽象类与接口)
- Shift Operations on C