凸包初學(POJ1113)

2021-07-03 21:47:34 字數 1216 閱讀 7378

第一道凸包的題目。
/*

凸包:對乙個簡單多邊形來說,如果給定其邊界上或者內部上的兩個點,連線著兩個點

連線這兩個點的線段上的所有點都被包含在該多邊形的邊界上或內部的話,則該多邊形為凸多邊形 。

給你一些點,用這些點連出凸多邊形,要求所有點都在所連成的凸多邊形邊界或內部。

選擇乙個左下角的左邊點 當做起始點。

鏈結p0與其他點,分別計算這些線段與「豎直向下方向」的夾角,

按照夾角由小到達的順序將各線段的另一端(一端是p0)標號為p1、p2、p3……

左轉判定

這是經典的計算幾何學問題,判斷向量p1=(x1,y1)到p2=(x2,y2)是否做左轉,只需要判斷x1*y2-x2*y1的正負,

如果結果為正,則從p1到p2做左轉。換句話說,若兩向量起點相同,p2在p1逆時針180度內,即為極座標下p2在p1左邊,

可根據此進行點的排序。

準備堆疊:建立堆疊s,棧指標設為t,將0、1、2三個點壓入堆疊s;

只要向量s[t-1]-i到s[t-1]-s[t]做左轉

則t--(左轉說明直線s[t-1]-i在直線s[t-1]-s[t]的外部)

將i點入棧;

棧中點即為組成凸多邊形的點

*/#include#include#includeusing namespace std;

const int maxn=1000+10;

struct node

st[maxn],num[maxn];

int n;

double l;

int dis(node p1,node p2)

int multi(node p1,node p2,node p3)

bool cmp(node p1,node p2)//左轉判定

// puts("asdjl");

//for(int i=0; i<=e; i++)

//printf("%d %d\n",st[i].x,st[i].y);

double sum=0;

for(int i=0; i<=e; i++)

sum+=sqrt((double)dis(st[i],st[i==e?0:i+1]));

sum+=2.0*l*3.1415926;

printf("%.0lf\n",sum);

return 0;

}

poj 1113 凸包模板

include include include include include include using namespace std define n 1050 define pi acos 1.0 int n,r struct point point operator point b const...

凸包計算 POJ1113

題目中除了要計算凸包之外,還要求修理的城牆要和宮殿之間的距離不小於l,那麼也就是相對於宮殿長度之外,多了乙個一l為半徑的圓的距離,所以題目就得以解決 poj1113 include include include include using namespace std typedef long lo...

POJ 1113 凸包周長

題意 國王想建乙個周長最短的城牆,使牆的任意一點到城牆的距離都 大於 rr。求這面牆的周長。題解 凸包 水平序graham掃瞄法 顯然答案就是 凸包的周長 半徑為rr的圓的周長 view code 1 include 2 include 3 include 4 include 5 include 6...