现在的位置: 首页 > 综合 > 正文

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

2017年10月17日 ⁄ 综合 ⁄ 共 1129字 ⁄ 字号 评论关闭

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

思路:数据范围是可以怎么搞都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);
}

抱歉!评论已关闭.