leetcode 587. Erect the Fence 最短围栏 + 凸包优化
2017-12-21 09:05
633 查看
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.
本题题意很简单,就是寻找一个凸多边形来找到包含所有节点的,这个是经典的问题,这个是参考这个教程的做法[LeetCode] Erect the Fence 竖立栅栏
代码如下:
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.
本题题意很简单,就是寻找一个凸多边形来找到包含所有节点的,这个是经典的问题,这个是参考这个教程的做法[LeetCode] Erect the Fence 竖立栅栏
代码如下:
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> using namespace std; /* struct Point { int x; int y; Point() : x(0), y(0) {} Point(int a, int b) : x(a), y(b) {} }; */ class Solution { public: vector<Point> outerTrees(vector<Point>& points) { vector<Point> res; Point first = points[0]; int firstIdx = 0, n = points.size(); for (int i = 1; i < n; ++i) { if (points[i].x < first.x) { first = points[i]; firstIdx = i; } } res.push_back(first); Point cur = first; int curIdx = firstIdx; while (true) { Point next = points[0]; int nextIdx = 0; for (int i = 1; i < n; ++i) { if (i == curIdx) continue; int cross = crossProduct(cur, points[i], next); if (nextIdx == curIdx || cross > 0 || (cross == 0 && dist(points[i], cur) > dist(next, cur))) { next = points[i]; nextIdx = i; } } for (int i = 0; i < n; ++i) { if (i == curIdx) continue; int cross = crossProduct(cur, points[i], next); if (cross == 0) { if (check(res, points[i])) res.push_back(points[i]); } } cur = next; curIdx = nextIdx; if (curIdx == firstIdx) break; } return res; } int crossProduct(Point A, Point B, Point C) { int BAx = A.x - B.x; int BAy = A.y - B.y; int BCx = C.x - B.x; int BCy = C.y - B.y; return BAx * BCy - BAy * BCx; } int dist(Point A, Point B) { return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); } bool check(vector<Point>& res, Point p) { for (Point r : res) { if (r.x == p.x && r.y == p.y) return false; } return true; } };
相关文章推荐
- [LeetCode] Erect the Fence 竖立栅栏
- (几何)LeetCode Weekly Contest 32 D-Erect the Fence
- 587. Erect the Fence
- leetcode_587.Erect the Fence?待解决
- 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)
- 587. Erect the Fence
- The minimum distance between two convex polygons---两个凸包之间的最短距离---旋转卡壳法
- leetcode:the skyline problem
- LeetCode 389 Find the Difference (异或)
- LeetCode-492. Construct the Rectangle
- Unity3D -- 性能优化(2)(官方教程Diagnosing performance problems using the Profiler window翻译)
- 【leetcode】389. Find the Difference【E】
- leetcode 389 Find the Difference C++
- The Postal Worker Rings Once(UVA 117)最短路径—SPFA算法+邻接表
- [leetcode287] Find the Duplicate Number
- LeetCode:Find the Difference_389
- POJ 1873 The Fortified Forest 二进制枚举 + 凸包 (final水题)
- [leetcode] sudoku solver:暴力还是优化
- LeetCode之389. Find the Difference
- leetcode[Find the Difference]//待整理多种解法