ZOJ 1450 Minimal Circle 最小圆覆盖
来源:互联网 发布:全站简繁体转换js代码 编辑:程序博客网 时间:2024/05/16 11:52
经典的计算几何题目,最小圆覆盖。
最小的圆肯定落在三个点上,因此暴力枚举圆上的三个点即可。
点增量算法,复杂度O(n^3)
加入随机化,平均复杂度可以降到O(n^2)
三点的外接圆圆心的函数:
POINT circumcenter(POINT &a, POINT &b, POINT &c) { POINT ret; double a1=b.x-a.x, b1=b.y-a.y, c1=(a1*a1+b1*b1)/2; double a2=c.x-a.x, b2=c.y-a.y, c2=(a2*a2+b2*b2)/2; double d = a1*b2 - a2*b1; ret.x = a.x + (c1*b2-c2*b1)/d; ret.y = a.y + (a1*c2-a2*c1)/d; return ret;}
这道题目在HDU上面也有,但是两道题目代码不能互相AC,应该是数据的原因,各种精度问题,还是看重算法吧,暂且把数据无视了。
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <cstdlib>using namespace std;#define N 110struct POINT { double x, y;} p[N];int n;inline double dist(const POINT &a, const POINT &b) { return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));}POINT circumcenter(POINT &a, POINT &b, POINT &c) { POINT ret; double a1=b.x-a.x, b1=b.y-a.y, c1=(a1*a1+b1*b1)/2; double a2=c.x-a.x, b2=c.y-a.y, c2=(a2*a2+b2*b2)/2; double d = a1*b2 - a2*b1; ret.x = a.x + (c1*b2-c2*b1)/d; ret.y = a.y + (a1*c2-a2*c1)/d; return ret;}void solve() { random_shuffle(p, p+n); //随机化序列,std里面的随机函数 POINT c; double r = 0; for (int i=1; i<n; i++) { if (dist(p[i], c) <= r) continue; c = p[i]; r = 0; for (int j=0; j<i; j++) { if (dist(p[j], c) <= r) continue; c.x = (p[i].x+p[j].x)/2; c.y = (p[i].y+p[j].y)/2; r = dist(p[j], c); for (int k=0; k<j; k++) { if (dist(p[k], c) <= r) continue; c = circumcenter(p[i], p[j], p[k]); r = dist(p[i], c); } } } printf("%.2lf %.2lf %.2lf\n", c.x, c.y, r);}int main() { while (scanf(" %d", &n) == 1 && n) { for (int i=0; i<n; i++) scanf(" %lf %lf", &p[i].x, &p[i].y); solve(); } return 0;}
- ZOJ 1450 Minimal Circle 最小圆覆盖
- ZOJ 1450 Minimal Circle 点集的最小圆覆盖
- ZOJ 1450 Minimal Circle 点集的最小圆覆盖
- ZOJ1450 Minimal Circle 最小圆覆盖
- ZJU 1450 Minimal Circle(平面点集最小圆覆盖)
- zoj 1450 Minimal Circle
- zoj 1450 Minimal Circle(最小覆盖圆)
- ZOJ 1450 Minimal Circle 求最小覆盖圆
- zoj 1450 Minimal Circle【最小覆盖圆问题】
- ZOJ 1450 HDU 3007 (最小圆覆盖)
- zoj 1450 Minimal Circle(最小包围圆)
- ZOJ 3147 Dezider's Coverup【最小圆覆盖】【模板题】
- 最小圆覆盖
- 最小圆覆盖
- 最小圆覆盖
- 最小圆覆盖
- 最小圆覆盖
- 最小圆覆盖
- HDU 1709 (母函数)
- CentOS/Ubuntu学习
- Makefile
- em/console/aboutApplication 发生系统错误 2 系统找不到指定的文件。
- ArcGIS三维分析之ArcGlobe简要说明
- ZOJ 1450 Minimal Circle 最小圆覆盖
- Fast Paxos
- C#_在VS2010下进行单元测试
- 提高办公工作效率的Linux命令行秘诀
- 虚函数练习1
- [疯狂Java讲义精粹] 第十二章|网络编程
- 【新学期开始】——接下来的博客计划。
- VC6.0中如何彻底删除一个已经添加的类
- ArcGlobe更改缓存路径报错。