Space Ant(poj1410叉积的极角排序)
来源:互联网 发布:mysql的sql语句大全 编辑:程序博客网 时间:2024/06/06 07:00
题意:有一种蚂蚁,他只能向左转,他还要吃植物,给你一个图,图上有好多植物的坐标,为找到一条路径使得这只蚂蚁吃得最多的植物
思路:极角排序,选定一个最外围的一个点,怎么排序呢?
三个点的叉乘可以确定哪条直线在右面,一个点在另一点的右面,说明这个点更靠外面,就是根据这个叉乘排序选择第一个,然后再根据选择的点在排序,在选择第一个,这样一直选择下去
c++忘完了快,写了个三个参数的构造函数,当传2个参数时,他把我的第一个参数覆盖掉了,让我好找啊....惨痛的教训
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;struct Point{ int Num; double x,y; Point(double x = 0,double y = 0):x(x),y(y){} //Point(int num = 0,double x = 0,double y = 0):Num(num),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 - (Vector a, Vector 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) ;}double Dot(Vector a,Vector b) { return a.x*b.x + a.y*b.y ;}double Length(Vector a) { return sqrt(Dot(a,a)) ;}double Cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x ;}const double eps = 1e-8;int dcmp(double x){ if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;}Point p[100];int pos;bool cmp(Point a,Point b){ double tmp = Cross(a-p[pos],b-p[pos]); if(dcmp(tmp) > 0)return true; if(dcmp(tmp) == 0) return Length(p[pos]-a) < Length(p[pos]-b); else return false;}int main(){ int T; scanf("%d",&T); int n; while(T--) { scanf("%d",&n); double x,y; for(int j = 0;j< n;j++) { scanf("%d%lf%lf",&p[j].Num,&p[j].x,&p[j].y); if( p[j].y < p[0].y || (p[j].y == p[0].y && p[j].x < p[0].x) ) swap(p[0],p[j]); } pos = 0; for(int i = 1;i < n;i++) { sort(p+i,p+n,cmp); pos++; } printf("%d",n); for(int i = 0;i < n;i++) printf(" %d",p[i].Num); printf("\n"); } return 0;}
- Space Ant(poj1410叉积的极角排序)
- POJ 1696 Space Ant (极角排序的应用)
- poj-1696-Space Ant-极角排序
- poj 1696 :Space Ant (极角排序)
- Poj 1696 Space Ant 【极角排序】
- POj 1696 Space Ant 凸包(极角排序)
- POJ - 1696 Space Ant( 极角排序)
- POj 1696 Space Ant (极角排序)
- Poj.1696 Space Ant【极角排序】 2015/08/27
- POJ 1696Space Ant(极角排序)
- POJ 1696 Space Ant(极角排序)
- POJ 1696 Space Ant(极角排序)
- POJ1696---Space Ant(基础计算几何:极角排序)
- POJ1696 space ant(计算几何,极角排序)
- POJ 1696 Space Ant (计算几何 + 极角排序)
- poj1696 Space Ant (极角排序,叉积)
- POJ 1696 Space Ant(极角排序&&叉积)
- poj 1696 Space Ant(叉积的性质,做极角排序)
- ios项目种arc的支持于非支持
- Spring 注解@Transactional
- shell小实例
- 如何加载MyEclipse的其中一个JAVA项目
- My97 DatePicker日期时间控件的配置使用方式
- Space Ant(poj1410叉积的极角排序)
- in 和 exists , not in 和 not exists
- MongoDB——第三天 细说高级操作
- 在C++中使用sqlite的示例(从下载、编译、工程建立、配置、测试代码编写全方位介绍)
- 使用SQLServer 2008的CDC功能实现数据变更捕获
- 微软一站式示例代码库
- win 并口编程
- Oracle Orion使用
- include与jsp:include与s:action与s:include与iframe用法汇总