UVALive 6859——凸包&&周长
题目
题意:在一个网格图上,给出$n$个点的坐标,用一个多边形包围这些点(不能接触,且多边形的边只能是对角线或直线),求多边形的最小周长.
分析
对于每个点,我们考虑与之相邻的4个点。一共由 $4 \times N$ 个点,然后求凸包。对凸包上每对相邻的点,优先走对角线,然后走直线。累加长度即可。
#include<bits/stdc++.h>
using namespace std; struct Point{
double x, y;
Point(double x=, double y=):x(x), y(y){}
};
typedef Point 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);
}
Vector operator * (Vector A, double p){
return Vector(A.x * p, A.y * p);
}
Vector operator / (Vector A, double p){
return Vector(A.x / p, A.y / p);
}
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;
} int ConvexHull(Point* p, int n, Point* ch)
{
sort(p, p+n);
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;
} const int maxn = + ;
int n;
Point points[maxn * ], ch[maxn * ]; int main()
{
//printf("%lf\n", sqrt(2) * 4);
while(scanf("%d", &n) == )
{
double x, y;
int cnt = ;
for(int i = ;i < n;i++)
{
scanf("%lf%lf", &x, &y);
points[cnt].x = x+, points[cnt++].y = y;
points[cnt].x = x-, points[cnt++].y = y;
points[cnt].x = x, points[cnt++].y = y+;
points[cnt].x = x, points[cnt++].y = y-;
}
int pcnt = ConvexHull(points, cnt, ch);
double ans = ; //sort(ch, ch + pcnt); //for(int i = 0;i < pcnt;i++) printf("%d: %lf %lf\n", i, ch[i].x, ch[i].y); double dx = fabs(ch[].x - ch[pcnt-].x);
double dy = fabs(ch[].y - ch[pcnt-].y);
if(dx > dy) swap(dx, dy);
ans += dx * sqrt();
ans += (dy - dx); for(int i = ;i < pcnt;i++)
{
double dx = fabs(ch[i].x - ch[i-].x);
double dy = fabs(ch[i].y - ch[i-].y);
if(dx > dy) swap(dx, dy);
ans += dx * sqrt();
ans += (dy - dx); //printf("%lf\n", ans);
}
printf("%lf\n", ans);
}
return ;
}
参考链接:https://blog.csdn.net/qingshui23/article/details/52809736
UVALive 6859——凸包&&周长的更多相关文章
- HDU 1392 凸包模板题,求凸包周长
1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...
- poj 1113:Wall(计算几何,求凸包周长)
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28462 Accepted: 9498 Description ...
- Wall---hdu1348(求凸包周长 模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 求凸包周长+2*PI*L: #include <stdio.h> #include ...
- POJ 1113 Wall(Graham求凸包周长)
题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 #include <stdi ...
- HDU 1392 Surround the Trees (Graham求凸包周长)
题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 #include <stdio.h> #include < ...
- poj 1113 凸包周长
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33888 Accepted: 11544 Descriptio ...
- LightOJ 1239 - Convex Fence 凸包周长
LINK 题意:类似POJ的宫殿围墙那道,只不过这道题数据稍微强了一点,有共线的情况 思路:求凸包周长加一个圆周长 /** @Date : 2017-07-20 15:46:44 * @FileNam ...
- hdu 1392:Surround the Trees(计算几何,求凸包周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1348:Wall(计算几何,求凸包周长)
Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- pptpd的log整理
前言: 最近有时候,我的pptpd会莫名崩掉.这时,在外边的我连不到内网,气的一比. 这时候,就需要去查一查log日志了. 所以就记录一下怎么调日志的: 1. 修改/etc/ppp/pptpd.o ...
- 1-2-K Game
题目链接:https://vjudge.net/contest/330119#problem/E 题目大意可以理解为: 1.给出n个物品以及k,Alice与Bob轮流拿1个,或2个,或k个物品,Ali ...
- [转帖]linux下CPU、内存、IO、网络的压力测试,硬盘读写速度测试,Linux三个系统资源监控工具
linux下CPU.内存.IO.网络的压力测试,硬盘读写速度测试,Linux三个系统资源监控工具 https://blog.51cto.com/hao360/1587165 linux_python关 ...
- tabs 导航 及内容切换
<!-- 导航头 --> <div class="col-md-6" style="padding: 0px"> <ul id=& ...
- web项目获取资源文件
首页 博客 学院 CSDN学院 下载 论坛 APP CSDN 问答 商城 活动 VIP会员 专题 招聘 ITeye GitChat GitChat 图文课 写博客 消息 1 评论 关注 点赞 回答 系 ...
- Scrapy payload 报错400
首先Scrapy 发送payload请求格式如下: def start_requests(self): querystr = { "ctoken": "U-ang1zmp ...
- 关于@JsonFormat(出参格式化)和@DateTimeFormat(入参格式化)
背景: 从数据库查询获取数据时候 返回的json数据 日期会出现一串数字或者其他形式 和我们期待的不一样 如下图: 一开始使用@DateTimeFormat注解 但是输出结果和没有使用返回的jso ...
- jupyter的控件交互
jupyter实现控件交互 jupyter notebook 是一个交互式IDE 直接上jupyter notebook界面截图
- Python爬虫—requests库get和post方法使用
目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...
- javascript 构建模块化开发
在使用 sea.js .require.js . angular 的时候. 我们使用到 define . module(require) 的方式,定义模块,和依赖模块 下面给出 define 和 m ...