Description
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.
There are no more than 100 trees.
Input
Zero at line for number of trees terminates the input for your program.
Output
Sample Input
Sample Output
MicrosoftInternetExplorer402DocumentNotSpecified7.8Normal0
A題(Graphm求凸包)
實際上就是求一個凸包,不過要注意,n=2的時候,不要將總長度乘以2。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct point {
double x, y;
} pnt[105];
int n;
point res[105];
bool mult(point sp, point ep, point op) {
return(sp.x-op.x)*(ep.y-op.y) >= (ep.x-op.x)*(sp.y-op.y);
}
bool operator < (const point &l, const point &r) {
return l.y
}
double graham() {
int i, len, top = 1;
sort(pnt, pnt + n);
if(n == 0)
return 0;
res[0] = pnt[0];
if(n == 1) return 0;
res[1] = pnt[1];
if(n == 2)
return sqrt((res[1].x-res[0].x)*(res[1].x-res[0].x)+(res[1].y-res[0].y)*(res[1].y-res[0].y));
res[2] = pnt[2];
for(i=2; i
while(top && mult(pnt[i], res[top], res[top-1]))
top--;
res[++top] = pnt[i];
}
len = top;
res[++top] = pnt[n-2];
for(i=n-3; i>=0; i--) {
while (top!=len && mult(pnt[i], res[top], res[top-1]))
top--;
res[++top] = pnt[i];
}
double sum = 0;
for (int i=1; i<=top; i++) {
sum += sqrt((res[i].x-res[i-1].x)*(res[i].x-res[i-1].x) + (res[i].y-res[i-1].y)*(res[i].y-res[i-1].y));
}
return sum;
}
int main()
{
while (scanf("%d", &n) != EOF) {
if (n == 0) break;
for (int i=0; i
scanf("%lf %lf", &pnt[i].x, &pnt[i].y);
printf("%.2lf\n", graham());
}
return 0;
}
Graham's Scan法 求解凸包問題。
- #include
- #include
- #include
- #include
- using namespace std;
- #define N 1005
- struct Point
- {
- int x,y;
- }P[N],S[N];
- int n,top;
- double Dis(const Point& p1,const Point& p2)
- {
- return sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y)*1.0 );
- }
- double Cross(const Point& p1,const Point& p2,const Point& p3,const Point& p4)
- {
- return (p2.x-p1.x)*(p4.y-p3.y) - (p2.y-p1.y)*(p4.x-p3.x);
- }
- bool cmp(const Point& p1,const Point& p2)
- {
- double C = Cross(P[0],p1,P[0],p2);
- return C ? C>0 : Dis(P[0],p1)
- }
- void Pole_Point()
- {
- int id;
- Point pp = P[id=0];
- for(int i=1;i
- if(P[i].y < pp.y || P[i].y == pp.y && P[i].x < pp.x)
- pp = P[i] , id = i;
- P[id] = P[0] , P[0] = pp;
- }
- void Convex_Hull()
- {
- Pole_Point();
- sort(P+1,P+n,cmp);
- S[0]=P[0],S[top=1]=P[1];
- for(int i=2;i
- {
- while(top && Cross(S[top-1],S[top],S[top],P[i]) <= 0)
- --top;
- S[++top] = P[i];
- }
- }
- int main()
- {
- while(~scanf("%d",&n))
- {
- for(int i=0;i
- scanf("%d%d",&P[i].x,&P[i].y);
- Convex_Hull();
- for(int i=0;i<=top;i++)
- printf("%d %d\n",S[i].x,S[i].y);
- }
- return 0;
- }
//凸包struct point{ double x, y;};
bool mult(point sp, point ep, point op)
{ return(sp.x - op.x) * (ep.y - op.y) >= (ep.x - op.x) * (sp.y - op.y);}
bool operator <</FONT> (const point &l, const point &r)
{ return l.y <</FONT> r.y || (l.y == r.y && l.x <</FONT> r.x);}
int graham(point pnt[], intn, point res[])
{ int i, len, k = 0, top = 1;
sort(pnt, pnt + n);
if(n == 0)
return 0;
res[0] = pnt[0];
if(n == 1)
return 1;
res[1] = pnt[1];
if(n == 2) return 2;
res[2] = pnt[2];
for(i = 2; i <</FONT> n; i++)
{ while(top && mult(pnt[i], res[top], res[top-1])) top--; res[++top] = pnt[i]; }
len = top;
res[++top] = pnt[n - 2];
for(i = n - 3; i >= 0; i--)
{ while(top!=len && mult(pnt[i], res[top], res[top-1]))
top--;
res[++top] = pnt[i];
}
return top; // 返回凸包中點的個數}