UVA 12386 Smallest Polygon n個點的手繪多邊形求最小周長 科學的暴力,12386smallest

來源:互聯網
上載者:User

UVA 12386 Smallest Polygon n個點的手繪多邊形求最小周長 科學的暴力,12386smallest

題目連結:

題意:

給定n個點,用n個點組成的多邊形中(可以是凹多邊形,但n個點一定要全在多邊形上)

在所有能由n個點構成的多邊形中

求最小面積的多邊形的周長 - 最小周長。

思路:

首先我們選擇一個定點,則接下來的數進行一個排列,有(n-1)!個排列。

這個序列相鄰兩個數之間有一條線段。

判斷多邊形合法:任意兩條線段不相交即可。n^2

剩下就是簡單的更新答案了。

所以複雜度是 ( n-1 ) ! * n*n

#include <cstdio>#include <cstring>#include <algorithm>#include <set>#include <cmath>using namespace std;const int MAX_N = 2507;struct point {    double x, y;    point(double _x = 0, double _y = 0) {        x = _x, y = _y;    }    bool operator < (const point &rhs) const {        if (x != rhs.x) return x < rhs.x;        return y < rhs.y;    }};int n;int top;point st[100], p[100];typedef point Point;double cross(point p0, point p1, point p2) {    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);}double mult_cross(Point p1, Point p2, Point p0) {return (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x);}bool is_cross(Point p1, Point p2, Point p3, Point p4) {if (std::min(p1.x, p2.x) <= std::max(p3.x, p4.x) &&    std::min(p3.x, p4.x) <= std::max(p1.x, p2.x) &&std::min(p1.y, p2.y) <= std::max(p3.y, p4.y) &&std::min(p3.y, p4.y) <= std::max(p1.y, p2.y) &&mult_cross(p1, p4, p3) * mult_cross(p2, p4, p3) <= 0 &&mult_cross(p3, p2, p1) * mult_cross(p4, p2, p1) <= 0)return true;return false;}double dis(point p1, point p2) {    return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));}int b[100];bool check() {    b[n - 1] = n - 1;    for (int i = 0; i < n; ++i) {        for (int j = 0; j < n; ++j) {            if (i == j) continue;            int x = (i + 1) % n, y = (j + 1) % n;            if (i == y || j == x || x == y) continue;            if (is_cross(p[b[i]], p[b[x]], p[b[j]], p[b[y]]))                return false;        }    }    return true;}inline double Cross(const Point &a, const Point &b) {return a.x * b.y - a.y * b.x;}bool Equal(double a, double b) {return fabs(a - b) < 1e-6;}int main() {    int T;    scanf("%d", &T);    while (T-- > 0) {        scanf("%d", &n);        for (int i = 0; i < n; ++i) {            int x, y;            scanf("%d%d", &x, &y);            p[i].x = 1. * x, p[i].y = 1. * y;        }        if (n == 3) {            puts("0.0000");            continue;        }        for (int i = 0; i < n - 1; ++i) b[i] = i;        double minlength = -1, minarea = -1, minarealength = -1;        do {            if (check()) {                double area = 0, length = 0;                b[n] = b[0];                for (int i = 0; i < n; i++) {                    length += dis(p[b[i]], p[b[i+1]]);                    area += Cross(p[b[i]], p[b[i+1]]);                }                area = fabs(area) * 0.5;              //  printf("www %.4f %.4f\n", area, length);               // for (int i = 0; i < n; ++i) printf("(%d %.f %.f) ", b[i], p[b[i]].x, p[b[i]].y); puts("");                if (minlength == -1) {                    minlength = length;                    minarea = area;                    minarealength = length;                } else {                    minlength = min(minlength, length);                    if (minarea > area || (Equal(area, minarea) && length < minarealength)) {                        minarea = area;                        minarealength = length;                    }                }            }           // for (int i = 0; i < n; ++i) printf("%d ", b[i]);        } while (next_permutation(b, b + n - 1));      //  printf("%.4f %.4f %.4f\n", minlength, minarealength, minarea);        printf("%.4f\n", fabs(minarealength - minlength));    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.