zoj 1429 || poj 1696 Space Ant 计算几何叉积或凸包改进版

这篇计算几何的题一直没打琢磨明白,后来看到小媛的解题报告恍然大悟,就先收藏下

给一些点,这些点要用线连起来,实际上是一个外星生物由于身体结构特殊,只能逆时针走,就是说a--b--c只能往左偏,或者在一条直线上也行,起点是(0,min(y)); 求出最多走的点数,并且按顺序输出他们的ID,按照走的顺序!

注意地点的纵坐标最低,所以一定能将所有的点都走完……  注意这一点


下面是媛姐的博文,并配上我加了点注释的代码:

叉积是计算几何的关键,点的排序是凸包进行的前提进行的,排序貌似有两种,媛姐用的是极坐标排序


==============================下面是媛姐的原文,代码我加了的注释=======================================

一期分类上的,一直木有做。

确实是好题啊。这个题是输出ant走的点,相当于一圈一圈往里面走,螺旋走。

改编了凸包算法。

我的可能时间按复杂度高些,每走一圈,对最后一个点再进行极角排序,再求凸包。。。就这么求下去。。。

= =。。刚搜了下题解,基本大家都差不多,一直这么做极角排序。。

现在POJ的C++挂了,G++可AC。。



#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define BUG puts("here!!!")

using namespace std;

const int MAX = 51;

struct point { double x,y; int id;};
point c[MAX];
int out[MAX];
bool used[MAX];
const double eps = 1e-6;
bool dy(double x,double y)	{	return x > y + eps;}	// x > y  这里比较大小的地方是个小细节,得注意
bool xy(double x,double y)	{	return x < y - eps;}	// x < y
bool dyd(double x,double y)	{ 	return x > y - eps;}	// x >= y
bool xyd(double x,double y)	{	return x < y + eps;} 	// x <= y
bool dd(double x,double y) 	{	return fabs( x - y ) < eps;}  // x == y
double disp2p(point a,point b) //  a b 两点之间的距离
{
	return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
// 叉积 (三点)
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向,大于0就是顺时针方向
{
	return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
bool cmp(point a,point b)  // 排序
{///规则是按照左侧逆时针的优先,在同一个直线上的距离近的优先
    double len = crossProduct(c[0],a,b);
    if( dd(len,0.0) )///如果是true表示等于0
        return xy(disp2p(c[0],a),disp2p(c[0],b));///x小于y
    return xy(len,0.0);
}
point stk[MAX];
int top, cnt;
void Graham(int n)
{
    int tmp = 0;
    for(int i=1; i<n; i++)
    	if( xy(c[i].y,c[tmp].y) )///如果找出最小的y,使得(0,y)作为起点
    		tmp = i;
    ///上面的for是找到x最小的那点

    swap(c[0],c[tmp]);
    sort(c+1,c+n,cmp);
    cnt = 0;

    memset(used, false, sizeof(used));
    used[c[0].id] = used[c[1].id] = true;
    out[cnt++] = c[0].id;
    out[cnt++] = c[1].id;
    stk[0] = c[0]; stk[1] = c[1];
    top = 1;
    int i = 2;
    while( cnt < n )
    {///一圈圈的跑,跑完一圈不能往下跑之后更换节点继续往下跑!
		if( used[c[i].id] )
		{
			i++;
			if( i == n )
			{
				c[0] = stk[top];
				sort(c+1, c+n, cmp);
			}
			i %= n;
			continue;
		}
		while( top >= 1&& xyd( crossProduct(stk[top],stk[top-1],c[i]), 0.0 )  )///如果是逆时针方向并且top>=1
		{///如果不符合要求就回溯,回溯到这一圈的最后一个点就行,实际上回溯一个点就行;
		    ///然后从这个点在看时下一轮
			used[stk[top].id] = false;
			top--; cnt--;
		}
		stk[++top] = c[i];
		used[c[i].id] = true;
		out[cnt++] = c[i].id;
		i++;
		if( i == n )
		{
			c[0] = stk[top];///更换开始节点,用新的开始节点重新往下跑
			sort(c+1, c+n, cmp);
		}
		i %= n;
	}
}
int main()
{
	int ncases, n;

	scanf("%d", &ncases);

	while( ncases-- )
	{
		scanf("%d", &n);
		for(int i=0; i<n; i++)
			scanf("%d%lf%lf", &c[i].id, &c[i].x, &c[i].y);

		Graham(n);

		printf("%d",n);
		for(int i=0; i<cnt; i++)
			printf(" %d", out[i]);
		printf("\n");
	}

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值