zoukankan      html  css  js  c++  java
  • (几何)LeetCode Weekly Contest 32 D-Erect the Fence

    587. Erect the Fence

     
    • User Accepted: 78
    • User Tried: 196
    • Total Accepted: 82
    • Total Submissions: 452
    • Difficulty: Hard

    There are some trees, where each tree is represented by (x,y) coordinate in a two-dimensional garden. Your job is to fence the entire garden using the minimum length of rope as it is expensive. The garden is well fenced only if all the trees are enclosed. Your task is to help find the coordinates of trees which are exactly located on the fence perimeter.

    Example 1:

    Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
    Output: [[1,1],[2,0],[4,2],[3,3],[2,4]]
    Explanation:
    

    Example 2:

    Input: [[1,2],[2,2],[4,2]]
    Output: [[1,2],[2,2],[4,2]]
    Explanation:
    
    Even you only have trees in a line, you need to use rope to enclose them. 
    

    Note:

    1. All trees should be enclosed together. You cannot cut the rope to enclose trees that will separate them in more than one group.
    2. All input integers will range from 0 to 100.
    3. The garden has at least one tree.
    4. All coordinates are distinct.
    5. Input points have NO order. No order required for output.
     1 class Solution {
     2 public:
     3     /*计算两点之间距离*/
     4     static int dis(Point p1,Point p2)
     5     {
     6         return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
     7     }
     8     /*利用×乘 计算三点之间关系 0:共线 1:依次成顺时针 2:依次成逆时针*/
     9     static int orientation(Point p1,Point p2,Point p3)
    10     {
    11         int val=(p3.y-p1.y)*(p2.x-p1.x)-(p3.x-p1.x)*(p2.y-p1.y);
    12         if(val==0)
    13             return 0;
    14         else
    15             return val>0?2:1;
    16     }
    17     /*将点排序 采用结构体进行排序 第一次见*/
    18     struct pointsComparator
    19     {
    20         Point p0;/*基准点*/
    21         bool operator() (const Point& p1,const Point&  p2)
    22         {
    23             int val=orientation(p0,p1,p2);
    24             if(val==0)
    25                 return dis(p0,p1)<=dis(p0,p2);
    26             else
    27                 return val==2;
    28         }
    29         pointsComparator(Point p): p0(p){}
    30     };
    31     vector<Point> outerTrees(vector<Point>& points) {
    32         int n=points.size();
    33         if(n<=3)
    34             return points;
    35         int ymin=points[0].y,minlo=0;/*找y坐标最小的点,记录其下标*/
    36         for(int i=1;i<n;++i)
    37         {
    38             if(points[i].y<ymin||(points[i].y==ymin&&points[i].x<points[minlo].x))
    39                 ymin=points[i].y,minlo=i;
    40         }
    41         Point tem=points[0];
    42         points[0]=points[minlo];
    43         points[minlo]=tem;
    44         Point p0=points[0];
    45         sort(points.begin(),points.end(),pointsComparator(p0));
    46         
    47         
    48         Point pn = points.back();
    49         if (orientation(p0, points[1], pn) != 0) {//非所有点都共线
    50             int idx = n-1;
    51             while (orientation(p0, points[idx], pn) == 0) {//找到
    52                 idx--;
    53             }
    54             reverse(points.begin() + idx + 1, points.end());//调序??为啥
    55         }
    56         
    57         
    58         vector<Point> vertices;
    59         vertices.push_back(points[0]);
    60         vertices.push_back(points[1]);
    61         vertices.push_back(points[2]);
    62         for(int i=3;i<n;++i)
    63         {
    64             while(orientation(vertices[vertices.size()-2],vertices.back(),points[i])==1)
    65                 vertices.pop_back();
    66             vertices.push_back(points[i]);
    67         }
    68         return vertices;
    69     }
    70 };
  • 相关阅读:
    POJ-3131-Cubic Eight-Puzzle(双向BFS+哈希)
    Android WebView与JavaScript交互操作(Demo)
    程序猿Web面试之jQuery
    Java数据类型(基本数据类型和引用数据类型)
    Swift学习——A Swift Tour 枚举和结构体
    大龙的学习笔记之“虚方法,抽象方法,重写,抽象类,接口”
    thinkphp curd的事务回滚 一看就会
    图书源代码下载: Modern Differential Geometry of CURVES and SURFACES with Mathematica
    UOJ#422. 【集训队作业2018】小Z的礼物
    删除所选项(附加搜索部分的jquery)
  • 原文地址:https://www.cnblogs.com/quintessence/p/6852902.html
Copyright © 2011-2022 走看看