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;
}



發佈了76 篇原創文章 · 獲贊 9 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章