(几何)LeetCode Weekly Contest 32 D-Erect the Fence
2017-05-14 16:36
423 查看
587. Erect the Fence
User Accepted: 78User 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:
All trees should be enclosed together. You cannot cut the rope to enclose trees that will separate them in more than one group.
All input integers will range from 0 to 100.
The garden has at least one tree.
All coordinates are distinct.
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 };
相关文章推荐
- LeetCode Weekly Contest 40(2) - Solve the Equation
- leetcode 587. Erect the Fence 最短围栏 + 凸包优化
- LeetCode Weekly Contest 32 解题报告
- [LeetCode] Erect the Fence 竖立栅栏
- 73th LeetCode Weekly Contest Escape The Ghosts
- LeetCode Weekly Contest 32解题思路
- LeetCode Weekly Contest 32
- LeetCode Weekly Contest 25 之 545.Boundary of Binary Tree
- LeetCode Weekly Contest 23
- LeetCode Weekly Contest 23 之 541. Reverse String II
- LeetCode Weekly Contest 13-HammingDistance【易】
- LeetCode Weekly Contest 31解题思路
- LeetCode Weekly Contest 25 之 537.Complex Number Multiplication
- LeetCode Weekly Contest 9 第九周周赛
- LeetCode Weekly Contest 28解题思路
- LeetCode Weekly Contest 29解题思路
- LeetCode Weekly Contest 26解题思路
- 【LeetCode】LeetCode Weekly Contest 16B
- LeetCode Weekly Contest 25
- LeetCode Weekly Contest 8