hdu 1392 fzu 1333 Surround the Trees 简单凸包

使用凸包模版即可。

hdu 1392

/*
* hdu1392.c
*
* Created on: 2011-9-2
* Author: 王竹
*/

#include
<stdio.h>
#include
<math.h>
#include
<stdlib.h>
#define nmax 1001
#define eps 1e-8
typedef
struct point {
int x, y;
} point;
point Point[nmax];
int stack[nmax];
double res;
int cross_product(point p1, point p2, point p0) {
return (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x);
}
double pDistans(point p0, point p1) {
return sqrt((p0.x - p1.x) * (p0.x - p1.x) + (p0.y - p1.y) * (p0.y - p1.y));
}
int cmp(const void *a, const void *b) {
point n
= *(point *) a;
point m
= *(point *) b;
double temp;
temp
= cross_product(n, m, Point[0]);
if (temp > 0) {
return 1;
}
else if ((fabs(temp) < eps)
&& (pDistans(m, Point[0]) >= pDistans(n, Point[0]))) {
return 1;
}
return -1;
}
void solve(int n) {
int i;
for (i = 0, res = 0.0; i < n; i++) {
res
+= pDistans(Point[stack[i]], Point[stack[(i + 1) % n]]);
}
printf(
"%.2lf\n", res);
}
void grahamScan(int n) {
int i, k, top;
point p;
p
= Point[0], k = 0;
for (i = 1; i < n; i++) {
if (p.y > Point[i].y || (p.y == Point[i].y && p.x > Point[i].x)) {
p
= Point[i], k = i;
}
}
p
= Point[0], Point[0] = Point[k], Point[k] = p;
qsort(Point
+ 1, n - 1, sizeof(Point[0]), cmp);
for (i = 0; i <= 2; i++) {
stack[i]
= i;
}
for (i = 3, top = 2; i < n; i++) {
while (cross_product(Point[stack[top - 1]], Point[i], Point[stack[top]])
<= 0) {
top
--;
if (!top) {
break;
}
}
top
++;
stack[top]
= i;
}
solve(top
+ 1);
}
int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
int n, i;
while (scanf("%d", &n) != EOF,n) {
for (i = 0; i < n; i++) {
scanf(
"%d %d", &Point[i].x, &Point[i].y);
}
if (n == 1) {
puts(
"0.00");
continue;
}
if (n == 2) {
printf(
"%.2lf\n", pDistans(Point[0], Point[1]));
continue;
}
grahamScan(n);
}
return 0;
}

fzu 1333

/*
* hdu1392.c
*
* Created on: 2011-9-2
* Author: 王竹
*/

#include
<stdio.h>
#include
<math.h>
#include
<stdlib.h>
#define nmax 1001
#define eps 1e-8
typedef
struct point {
int x, y;
} point;
point Point[nmax];
int stack[nmax];
double res;
int cross_product(point p1, point p2, point p0) {
return (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x);
}
double pDistans(point p0, point p1) {
return sqrt((p0.x - p1.x) * (p0.x - p1.x) + (p0.y - p1.y) * (p0.y - p1.y));
}
int cmp(const void *a, const void *b) {
point n
= *(point *) a;
point m
= *(point *) b;
double temp;
temp
= cross_product(n, m, Point[0]);
if (temp > 0) {
return 1;
}
else if ((fabs(temp) < eps)
&& (pDistans(m, Point[0]) >= pDistans(n, Point[0]))) {
return 1;
}
return -1;
}
void solve(int n) {
int i;
for (i = 0, res = 0.0; i < n; i++) {
res
+= pDistans(Point[stack[i]], Point[stack[(i + 1) % n]]);
}
printf(
"%.2lf\n", res);
}
void grahamScan(int n) {
int i, k, top;
point p;
p
= Point[0], k = 0;
for (i = 1; i < n; i++) {
if (p.y > Point[i].y || (p.y == Point[i].y && p.x > Point[i].x)) {
p
= Point[i], k = i;
}
}
p
= Point[0], Point[0] = Point[k], Point[k] = p;
qsort(Point
+ 1, n - 1, sizeof(Point[0]), cmp);
for (i = 0; i <= 2; i++) {
stack[i]
= i;
}
for (i = 3, top = 2; i < n; i++) {
while (cross_product(Point[stack[top - 1]], Point[i], Point[stack[top]])
<= 0) {
top
--;
if (!top) {
break;
}
}
top
++;
stack[top]
= i;
}
solve(top
+ 1);
}
int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
int n, i;
while (scanf("%d", &n) != EOF,n) {
for (i = 0; i < n; i++) {
scanf(
"%d %d", &Point[i].x, &Point[i].y);
}
if (n == 1) {
puts(
"0.00");
continue;
}
if (n == 2) {
printf(
"%.2lf\n", pDistans(Point[0], Point[1]) * 2.0);
continue;
}
grahamScan(n);
}
return 0;
}
原文地址:https://www.cnblogs.com/xiaoxian1369/p/2164610.html