码迷,mamicode.com
首页 > 其他好文 > 详细

LC 963. Minimum Area Rectangle II

时间:2018-12-23 23:53:54      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:move   tor   mini   red   nbsp   The   HERE   90度   points   

Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these points, with sides not necessarily parallel to the x and y axes.

If there isn‘t any rectangle, return 0.

 

Example 1:

Input: [[1,2],[2,1],[1,0],[0,1]]
Output: 2.00000
Explanation: The minimum area rectangle occurs at [1,2],[2,1],[1,0],[0,1], with an area of 2.

Input: [[0,1],[2,1],[1,1],[1,0],[2,0]]
Output: 1.00000
Explanation: The minimum area rectangle occurs at [1,0],[1,1],[2,1],[2,0], with an area of 1.

这题也卡住了,主要是如何判断平面上4个点是矩形?

可以先判断它是个平行四边形,然后判断它的一个角是90度。

x0,x1,x2,x3
y0,y1,y2,y3

首先在不知道顺序的情况下,需要用全部的次序遍历。就是说1,2,3,4; 1,2,4,3;1,4,2,3;1,4,3,2的全排列。

其次判断直角。


#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

class Solution {
public:
 double minAreaFreeRect(vector<vector<int>>& points) {
   unordered_map<int, unordered_set<int>> c;
   for (auto &x:points)c[x[0]].insert(x[1]);
   long n = points.size(), x0, y0, x1, y1,x2,y2,x3,y3, r = LONG_MAX;
   REP(i, n) {
     x0 = points[i][0]; y0 = points[i][1];
     REP(j, n) {
       x1 = points[j][0]; y1 = points[j][1];
       REP(k, n)
       if (k != i && k != j) {
         x2 = points[k][0]; y2 = points[k][1];
         REP(l, n)
         if (l != i && l != j && l != k) {
           x3 = points[l][0]; y3 = points[l][1];
           if (x1-x0==x2-x3 && y1-y0==y2-y3 && x3-x0==x2-x1 && y3-y0==y2-y1)
             if ((x1-x0)*(x3-x0)+(y1-y0)*(y3-y0)==0)
               r = min(r, abs((x1 - x0) * (y3 - y0) - (y1 - y0) * (x3 - x0)));
         }
       }
     }
   }
   return r == LONG_MAX ? 0 : r;
 }

};

 






LC 963. Minimum Area Rectangle II

标签:move   tor   mini   red   nbsp   The   HERE   90度   points   

原文地址:https://www.cnblogs.com/ethanhong/p/10165825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!