POJ 1696 Space Ant 计算几何 叉积的应用

题目大意:平面内有一些点,我们要通过一些方式来走遍这所有的点,要求一个点只能走一次,只能向左转而不能向右转。求遍历这些点的顺序。


思路:数据范围是可以怎么搞都0ms的(n<=50,case<=100),所以只要有思路就可以了。

只能左转,想想好像有点像凸包啊。但是这个题要遍历所有的点,所以就把已经走过的点删掉,然后像凸包一样的往前走,每次找一个没走过的极角最小的点走,然后把它标记上。最后都走完就全部遍历完了。


CODE:


#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 110
#define INF 0x7f7f7f7f
using namespace std;

struct Point{
	int _id;
	double x,y;
	double alpha;

	Point(double _x,double _y):x(_x),y(_y) {}
	Point() {}
	Point operator -(const Point &a) {
		return Point(x - a.x,y - a.y);
	}
	bool operator <(const Point &a)const {
		return alpha < a.alpha;
	}
	void Read() {
		scanf("%d%lf%lf",&_id,&x,&y);
	}
	bool Cross(Point &a,Point &b) {
		Point p1 = *this - a,p2 = *this - b;
		return p1.x * p2.y - p2.x * p1.y < 0;
	}
}point[MAX],now;

int cases,points;

int rubbish;

inline void Initialize();

int main()
{
	for(cin >> cases;cases; --cases) {
		scanf("%d",&points);
		Initialize();
		for(int x,y,i = 1;i <= points; ++i) { 
			point[i].Read();
			if(point[i].y < now.y)
				now = point[i];
		}
		printf("%d",points);
		now.x = 0;
		point[0] = now;
		for(int i = 1;i <= points; ++i) {
			for(int j = i + 1;j <= points; ++j)
				if(point[j].Judge(point[i],point[i - 1]))
					swap(point[i],point[j]);
			printf(" %d",point[i]._id);
		}
		puts("");
	}
	return 0;
}

inline void Initialize()
{
	now = Point(0,INF);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值