POJ - 1696 Space Ant(极角排序)

题目链接:点击查看

题目大意:现在有一只特殊的蚂蚁,它会按照以下规则尽可能长的寻找路径:

  1. 不能回头
  2. 不能右转
  3. 只能逆时针行走

现在给出n个点,输出最长的路径

题目分析:既然是逆时针旋转,那么每次只能走极角最小的一个,每次都排序找就可以了,时间复杂度是n*n*logn,极角排序的cmp函数还是用这张图

代码:

#include<iostream>
#include<cstdio> 
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;
 
typedef long long LL;
 
const int inf=0x3f3f3f3f;

const int N=110;

const double eps = 1e-8;

int sgn(double x){
	if(fabs(x) < eps)return 0;
	if(x < 0)return -1;
	else return 1;
}

struct Point{
	double x,y;
	int id;
	Point(){}
	Point(double _x,double _y){
		x = _x;
		y = _y;
	}
	void input(){
		scanf("%d%lf%lf",&id,&x,&y);
	}
	bool operator < (Point b)const{
		return sgn(y-b.y)== 0?sgn(x-b.x)<0:y<b.y;
	}
	Point operator -(const Point &b)const{
		return Point(x-b.x,y-b.y);
	}
	//叉积
	double operator ^(const Point &b)const{
		return x*b.y - y*b.x;
	}
	//点积
	double operator *(const Point &b)const{
		return x*b.x + y*b.y;
	}
	//返回两点的距离
	double distance(Point p){
		return hypot(x-p.x,y-p.y);
	}
}point[N];

double xmult(Point p0,Point p1,Point p2)
{
	return (p1-p0)^(p2-p0);
}

int pos;

bool cmp(Point a,Point b)
{
	double temp=xmult(point[pos],a,b);
	if(sgn(temp)==0)
		return a.distance(point[pos])<b.distance(point[pos]);
	return sgn(temp)>0;
}

int main()
{
//	freopen("input.txt","r",stdin);
//	ios::sync_with_stdio(false);
	int w;
	cin>>w;
	while(w--)
	{
		int n;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			point[i].input();
			if(point[i]<point[0])
				swap(point[i],point[0]);
		}
		pos=0;
		for(int i=1;i<n;i++)
		{
			sort(point+i,point+n,cmp);
			pos++;
		}
		printf("%d",n);
		for(int i=0;i<n;i++)
			printf(" %d",point[i].id);
		putchar('\n');
	}
	
	
 
 
 
 
 
 
 
	
	
	
	
	
	
	
	
	
	return 0;
}
发布了577 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/104096091
今日推荐