【HDOJ】1348 Wall
2024-10-09 02:46:12
计算几何-凸包模板题目,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 ;
}
最新文章
- python爬虫小项目实战
- Java中,关于字符串类型、随机验证码、 时间类型
- PDO连接mysql和pgsql数据库
- js 四舍五入函数 toFixed(),小数位数精度
- 扩展Unity的方法
- mysql 用sql 语句去掉某个字段重复值数据的方法
- VMware Workstation linux 问题
- WPF 界面控件遍历和后台行为绑定写法
- ubuntu:configure error:cannot find ssl libraries
- 解决ZF2_PATH environment
- ASP.NET弹出提示点击确定之后再跳转页面的方法
- WebPack引用Bootstrap 无法使用图标的结局方案
- 20175320 2018-2019-2 《Java程序设计》第8周学习总结
- bash python获取文本中每个字符出现的次数
- PLSQL乱码
- idea常用实用快捷键
- IT小小鸟 读书笔记
- Spring MVC表单处理
- 复制Map对象:Map.putAll方法
- Hibernate的二级缓存(SessionFaction的外置缓存)-----Helloword
热门文章
- linux常用命令 http://mirrors.163.com/ubuntu-releases/12.04/
- Reso | Noise 网易云音乐插件
- centos6.3 配置NTP服务
- JavaScript--时间显示小插件
- node.js常用的几个模块总结
- 关于timestamp的二三事
- VS2008试用版到期解决办法
- MySQL无法登录服务器解决方法
- get the text value of a selected option.
- .NET Framework 中的类型系统的两个基本点