简单凸包
来源:互联网 发布:有网络但下载速度为零 编辑:程序博客网 时间:2024/05/29 03:18
凸包:
平面内有n个点,选取最外面的点并连接,得到的凸多边形就是凸包。
graham算法:
选取最左端与最右端的两个点,然后从左到右扫一遍,再从右到左扫一遍,分别建立凸包的上侧与下侧。
实现过程:
1.将各点按横坐标的值排序;
2.从第一个点到最后一个点,正向反向分别遍历一次,通过比较斜率来取舍外围点。
例题:
poj : http://poj.org/problem?id=2187
AC代码:
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int M = 50020;struct Point{ int x, y; bool operator < (const Point &i) const { if(x == i.x) return y < i.y; return x < i.x; }}dx[M], out[M];bool gradjudge(Point a, Point b, Point c){ int t1 = (a.x - b.x) * (c.y - b.y); int t2 = (a.y - b.y) * (c.x - b.x); return t1 >= t2;}int dist(Point a, Point b){ int res = (a.y - b.y) * (a.y - b.y) + (a.x - b.x) * (a.x - b.x); return res;}main(){ int n, k; while(~scanf("%d", &n)) { for(int i = 0; i < n; i++) scanf("%d %d", &dx[i].x, &dx[i].y); sort(dx, dx + n); k = 0; for(int i = 0; i < n; i++) { while(k > 1 && !gradjudge(out[k - 2], out[k - 1], dx[i])) k--; out[k++] = dx[i]; } int t = k; for(int i = n - 2; i >= 0; i--) { while(k > t && !gradjudge(out[k - 2], out[k - 1], dx[i])) k--; out[k++] = dx[i]; } int res = 0; for(int i = 0; i < k; i++) for(int j = i + 1; j < k; j++) res = max(res, dist(out[i], out[j])); printf("%d\n", res); }}/*40 00 11 11 050 01 12 20 22 0100 010000 01 1002 1999999 1009998 199100 -900200 -17999800 -17999900 -900*//*28100000000*/
0 0
- 简单凸包
- poj 2187 简单凸包
- hdu2907 凸包+简单搜索
- POJ1113 wall 简单凸包
- UVA10652凸包简单应用
- 凸包---(简单概念)
- HDU1348 Wall 【简单凸包】
- poj 1113 wall 简单的凸包
- poj 1113 凸包+简单几何计算
- HDU1392 Surround the Trees 简单凸包
- ZOJ 3537 简单凸包+DP
- UVA 10652 Board Wrapping 简单凸包
- UVA 10652 凸包简单问题
- poi包简单应用
- rmp4包简单介绍
- RMP4包简单介绍
- 简单包传输协议
- 简单JAR包制作
- FFMPEG架构分析
- MVVM 后台代码调用viewModel方法
- Hadoop分布式环境下的数据抽样
- 电信:CDMA设备淘汰不影响手机信号
- java的public, protected, private, friendly
- 简单凸包
- provider: Named Pipes Provider, error: 40 - 无法打开到 SQL Server 的连接
- POJ 2778 DNA Sequence
- 【mysql】mysql 常用建表语句 .
- 安装VS2010 SP1时出现回滚 问题解决
- JVM内存管理及垃圾回收
- Android 通过HttpClient访问服务器获取XML
- 马林强调位置保护
- 4G时代,软件开发工程师的又一春