zoj 1429 || poj 1696 Space Ant

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

确实是好题啊。这个题是输出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 的方向 
{
	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) )  
        return xy(disp2p(c[0],a),disp2p(c[0],b));  
    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) )
    		tmp = i;
    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( 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、付费专栏及课程。

余额充值