题目

链接

题意:在一个网格图上,给出$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——凸包&&周长的更多相关文章

  1. HDU 1392 凸包模板题,求凸包周长

    1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...

  2. poj 1113:Wall(计算几何,求凸包周长)

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28462   Accepted: 9498 Description ...

  3. Wall---hdu1348(求凸包周长 模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 求凸包周长+2*PI*L: #include <stdio.h> #include ...

  4. POJ 1113 Wall(Graham求凸包周长)

    题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 #include <stdi ...

  5. HDU 1392 Surround the Trees (Graham求凸包周长)

    题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 #include <stdio.h> #include < ...

  6. poj 1113 凸包周长

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33888   Accepted: 11544 Descriptio ...

  7. LightOJ 1239 - Convex Fence 凸包周长

    LINK 题意:类似POJ的宫殿围墙那道,只不过这道题数据稍微强了一点,有共线的情况 思路:求凸包周长加一个圆周长 /** @Date : 2017-07-20 15:46:44 * @FileNam ...

  8. hdu 1392:Surround the Trees(计算几何,求凸包周长)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. hdu 1348:Wall(计算几何,求凸包周长)

    Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. 2019牛客多校赛第一场 补题 I题

    I题  Points Division 题意: 给你n个点,每个点有坐标(xi,yi)和属性(ai,bi),将点集划分为两个集合, 任意 A 集合的点 i 和 B 集合点 j, 不允许 xi > ...

  2. Django soft-delete软删除

    在django中,实现这个功能很简单,我们采用一个字段用来保存删除的时间.若记录没有被删除,那么设置该值为None,如果被删除,那么设置时间为删除的时间. class BaseSchema(model ...

  3. kettle下载地址

    kettle 4.4和4.2 版本是好的,版本6.6和8.2 版本有bug Kettle下载和安装: 1.官网各个版本下载地址:https://sourceforge.net/projects/pen ...

  4. Mac安装postgresql和卸载PostgreSQL

    1.homebrew安装 brew install postgresql 2.初始化 initdb /usr/local/var/postgres 3.创建数据库及查看数据库 (1)先创建db. cr ...

  5. Simple Library Management System HDU - 1497(图书管理系统)

    Problem Description After AC all the hardest problems in the world , the ACboy 8006 now has nothing ...

  6. docker-compose搭建elasticsearch+kibana环境,以及php使用elasticsearch

    一.elasticsearch的Dockerfile 增加中文搜索插件analysis-ik FROM docker.elastic.co/elasticsearch/elasticsearch:7. ...

  7. re(模块正则表达式)

    re模块(正则) ​ 正则是用一些具有特殊含义的符号组合到一起(成为正则表达式)来描述字符或者字符串的方法,或者说正则就是用来描述一类事物的规则. import re #从字符串中全部查找内容,返回一 ...

  8. MyBatis 源码篇-整体架构

    MyBatis 的整体架构分为三层, 分别是基础支持层.核心处理层和接口层,如下图所示. 基础支持层 反射模块 该模块对 Java 原生的反射进行了良好的封装,提供了更加简洁易用的 API ,方便上层 ...

  9. 排查RabbitMQ安装错误

    1.注册表中是否有  HKEY_LOCAL_MACHINE\SOFTWARE\Ericsson\Erlang\ErlSrv\1.1\RabbitMQ 此项.(须有) 2.安装目录是否存在中文.(不可有 ...

  10. asp.net 代码片段的

    片段标签                                                   说明 <%                                      ...