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);
}
时间: 2024-10-12 16:44:57

POJ 1696 Space Ant 计算几何 叉积的应用的相关文章

POJ 1696 Space Ant(点积的应用)

Space Ant 大意:有一只蚂蚁,每次都只向当前方向的左边走,问蚂蚁走遍所有的点的顺序输出.开始的点是纵坐标最小的那个点,开始的方向是开始点的x轴正方向. 思路:从开始点开始,每次找剩下的点中与当前方向所形成的夹角最小的点,为下一个要走的点(好像就是犄角排序,我不是很会),夹角就是用点积除以两个向量的距离,求一下acos值. 之前一直用叉积做,做了好久样例都没过,发现用错了... 题目挺好的,有助于理解点积与叉积 1 struct Point{ 2 double x, y; 3 int id

poj 1696 Space Ant (极角排序)

链接:http://poj.org/problem?id=1696 Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3077   Accepted: 1965 Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an a

poj 1696 Space Ant(模拟+叉积)

Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3840   Accepted: 2397 Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the planet Y19

poj 1696 Space Ant(极角排序)

Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3661   Accepted: 2281 Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the planet Y19

POJ 1696 Space Ant 卷包裹法

Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3316   Accepted: 2118 Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the planet Y19

POJ 1696 Space Ant (极角排序)

题目链接 Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3219   Accepted: 2059 Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the plane

POJ 1696 Space Ant

极角排序 每次选择一个最外围的没选过的点,选择的时候需要利用极角排序进行选择 #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<list> #include<algorithm> using namespace std; const double eps=1e-8; struct poin

POJ 1696 Space Ant --枚举,模拟,贪心,几何

题意: 有很多点,从最右下角的点开始走起,初始方向水平向右,然后以后每步只能向左边走,问最多能走多少个点. 解法: 贪心的搞的话,肯定每次选左边的与它夹角最小的点,然后走过去. 然后就是相当于模拟地去选点,然后计数,然后走过去.这题就这么搞定了. 我这里用了set和vector. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include &

POJ 1696 Space Ant 【极角排序】

题意:平面上有n个点,一只蚂蚁从最左下角的点出发,只能往逆时针方向走,走过的路线不能交叉,问最多能经过多少个点. 思路:每次都尽量往最外边走,每选取一个点后对剩余的点进行极角排序.(n个点必定能走完,这是凸包的性质决定的) #include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> using namespace std;