http://acm.hdu.edu.cn/showproblem.php?pid=1348
典型凸包题,求外围城墙的周长
Sample Input
1
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
Sample Output
1628
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <set>
//#include <map>
#include <queue>
#include <utility>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;
#define PI 3.14159265
struct point{
double x, y, angel;
}p[1005], ch[1005];
double dist (point a, point b)
{
return sqrt ((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
double multi (point a, point b, point c)
{
double x1, y1, x2, y2;
x1 = b.x - a.x;
y1 = b.y - a.y;
x2 = c.x - b.x;
y2 = c.y - b.y;
return x1*y2 - x2*y1;
}
bool cmp (point a, point b)
{
if (a.y == b.y)
return a.x < b.x;
return a.y < b.y;
}
bool cmp2 (point a, point b)
{
if (a.angel == b.angel)
{
if (a.x == b.x)
return a.y > b.y;
return a.x > b.x;
}
return a.angel < b.angel;
}
int main()
{
int n, i, top, t;
double r, len;
scanf ("%d", &t);
while (t--)
{
scanf ("%d%lf", &n, &r);
for (i = 0; i < n; i++)
scanf ("%lf%lf", &p[i].x, &p[i].y);
sort (p, p+n, cmp); //找到左下角的p[0]
//找相对于p[0]的极角,并把除了p[0]以外的点按照极角排序
for (i = 1; i < n; i++)
p[i].angel = atan2 (p[i].y-p[0].y, p[i].x-p[0].x);
sort (p+1, p+n, cmp2);
//Graham_Scan算法
ch[0] = p[0], ch[1] = p[1], ch[2] = p[2];
top = 3;
for (i = 3; i < n; i++)
{
while (top > 2 && multi (ch[top-2], ch[top-1], p[i]) <= 0)
top--;
ch[top++] = p[i];
}
//求周长
len = dist (ch[0], ch[top-1]);
for (i = 1; i < top; i++)
len += dist (ch[i], ch[i-1]);
len += 2 * PI * r; //加上圆弧,刚好为一个圆!
printf ("%.0lf\n", len);
if (t)
printf ("\n");
}
return 0;
}
分享到:
相关推荐
graham_scan_js, 在JavaScript中,Graham凸包算法的扫描实现 基于的凸壳算法的扫描为了从给定的x,y 坐标中计算凸船体,我需要一个简单的实现,我发现 hull js的凸点,或者需要其他库的依赖性。 这个实现只需要x,y ...
Graham Scan算法是一个用来求凸包的算法,采用java语言对该算法进行实现
在vs2010环境下凸包问题枚举 Graham_scan以及分治三种方法实现,并用matlab将结果画出来,进行验证。细节考虑较全面。
实现凸包实现,采用MATLAB编写代码,用于凸包算法的快速实现。
graham求凸包算法 graham求凸包算法 graham求凸包算法 graham求凸包算法 graham求凸包算法 graham求凸包算法
两种凸包算法 算法导论的详细实现 C++ VS2008
2.实现基于Graham-Scan的凸包求解算法 3.实现基于分治思想的凸包求解算法 4.界面图,可以用鼠标随便点,然后用这些点求出凸包并在界面上画出凸包源代码和实验报告!均为自己所做,正确运行。报告中还有用Excel表分析...
用graham算法在vc环境下编写的形成一个最小凸包程序,能够读入一个点文件,并输出一个点文件。
实现点集凸包的算法,把众多随机分布的点用一条折线以最小的长度使其全部包括其中
绘制图形轮廓,进行凹凸点挖掘算法,可以判断图形的凹凸性
grahamScan扫描法求凸包 输入有若干组测试数据。每一组测试数据的第一行上有整数n,表示该组测试数据有n个点组成的。接下来有n行,其每一行上有二个正整数,之间用一个或几个空格隔开。当输入行上只有一个数0时,...
2.正确求出导入数据散点集的凸包点。自定义网格的大小,在网格中绘制出凸包图形,并可保存为.dxf文件。 3.可以实现凸包图片在程序窗口的基本操作。 4.可自定义基准高程,求出凸包内高于基准高程的体积。并形成报告,...
计算凸包常用的一种算法--graham算法。
采用Graham扫描法,实现提取凸包最小边界JS代码实现。(代码中点坐标采用平面坐标,如要在三维贴地展示,可转换为经纬度坐标)
凸包的应用文件(包裹法求凸包的算法) 写得比较经典
c++实现的GraHam算法,解决凸包问题
实现了一个2维点集的凸包算法,其中用到快速排序。
java实现利用Graham算法解决凸包问题,带界面验证
这是我用matlab写的一个寻找凸包的算法实现,是一个基于graham算法改进的算法