zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    css div position to parent
    linux 解压缩/压缩命令大全
    button with backgroundimage programmaticaly
    使用数组初始化vector 对象
    AudioServicesPlaySystemSound
    objective-c 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
    指针和多维数组(例子需要好好消化理解)
    很经典的赋值算法之一:动态为数组有序赋值
    string 类的c_str 的成员函数
    自由存储区的空间 C++和C
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/2164610.html
Copyright © 2011-2022 走看看