计算几何-凸包模板题目,Graham算法解。

 /* 1348 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std; #define MAXN 1005 typedef struct Point_t {
double x, y;
Point_t() {}
Point_t(double xx, double yy) {
x = xx; y = yy;
}
} Point_t; Point_t stack[MAXN];
Point_t points[MAXN];
const double PI = acos(-1.0);
int n; double Length(Point_t a, Point_t b) {
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
} double cross(Point_t p0, Point_t p1, Point_t p2) {
return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
} bool comp(Point_t a, Point_t b) {
double k = cross(points[], a, b);
return !(k< || (k== && Length(points[], a)>Length(points[], b)));
} double Graham() {
double ret = ;
int i, j, k;
int top;
Point_t p0; p0 = points[];
k = ;
for (i=; i<n; ++i) {
if (points[i].x<p0.x || (points[i].x==p0.x && points[i].y<p0.y)) {
p0 = points[i];
k = i;
}
} points[k] = points[];
points[] = p0;
sort(points+, points+n, comp); stack[] = points[];
stack[] = points[];
stack[] = points[];
top = ;
for (i=; i<n; ++i) {
while (cross(stack[top-], stack[top], points[i])<= && top>)
--top;
stack[++top] = points[i];
}
stack[++top] = p0;
for (i=; i<=top; ++i)
ret += Length(stack[i-], stack[i]); return ret;
} int main() {
int t;
int i, j, k, tmp;
double l, ans; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif scanf("%d", &t);
while (t--) {
scanf("%d %lf", &n, &l);
for (i=; i<n; ++i)
scanf("%lf %lf", &points[i].x, &points[i].y);
ans = Graham();
ans += PI * (l+l);
printf("%.0lf\n", ans);
if (t)
printf("\n");
} return ;
}

最新文章

  1. python爬虫小项目实战
  2. Java中,关于字符串类型、随机验证码、 时间类型
  3. PDO连接mysql和pgsql数据库
  4. js 四舍五入函数 toFixed(),小数位数精度
  5. 扩展Unity的方法
  6. mysql 用sql 语句去掉某个字段重复值数据的方法
  7. VMware Workstation linux 问题
  8. WPF 界面控件遍历和后台行为绑定写法
  9. ubuntu:configure error:cannot find ssl libraries
  10. 解决ZF2_PATH environment
  11. ASP.NET弹出提示点击确定之后再跳转页面的方法
  12. WebPack引用Bootstrap 无法使用图标的结局方案
  13. 20175320 2018-2019-2 《Java程序设计》第8周学习总结
  14. bash python获取文本中每个字符出现的次数
  15. PLSQL乱码
  16. idea常用实用快捷键
  17. IT小小鸟 读书笔记
  18. Spring MVC表单处理
  19. 复制Map对象:Map.putAll方法
  20. Hibernate的二级缓存(SessionFaction的外置缓存)-----Helloword

热门文章

  1. linux常用命令 http://mirrors.163.com/ubuntu-releases/12.04/
  2. Reso | Noise 网易云音乐插件
  3. centos6.3 配置NTP服务
  4. JavaScript--时间显示小插件
  5. node.js常用的几个模块总结
  6. 关于timestamp的二三事
  7. VS2008试用版到期解决办法
  8. MySQL无法登录服务器解决方法
  9. get the text value of a selected option.
  10. .NET Framework 中的类型系统的两个基本点