Guest User

Untitled

a guest
Jan 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. struct Dot
  2. {
  3. int x, y;
  4.  
  5. Dot(int i, int j)
  6. {
  7. x = i;
  8. y = j;
  9. }
  10.  
  11. bool operator==(const Dot& that)const
  12. {
  13. return x == that.x && y == that.y;
  14. }
  15. };
  16.  
  17. struct Hasher
  18. {
  19. size_t operator()(const Dot& point)const
  20. {
  21. string str = to_string(point.x) + ":" + to_string(point.y);
  22. return hash<string>()(str);
  23. }
  24. };
  25.  
  26. class Solution {
  27. public:
  28. double minAreaFreeRect(vector<vector<int>>& points) {
  29. int len = points.size();
  30. unordered_set<Dot, Hasher> set;
  31. for(auto& point : points)
  32. {
  33. Dot p(point[0], point[1]);
  34. set.insert(p);
  35. }
  36. double res = 40005 * 40005;
  37. for(int i = 0; i < len; ++i)
  38. {
  39. Dot p0(points[i][0], points[i][1]);
  40. for(int j = 0; j < len; ++j)
  41. {
  42. if(j == i)continue;
  43. Dot p1(points[j][0], points[j][1]);
  44. for(int k = j + 1; k < len; ++k)
  45. {
  46. if(k== i)continue;
  47. Dot p2(points[k][0], points[k][1]);
  48. Dot p3(p1.x + p2.x - p0.x, p1.y + p2.y - p0.y);
  49. if(set.find(p3) != set.end() && isPerpen(p0, p1, p2))
  50. {
  51. double e1 = getLen(p0, p1), e2 = getLen(p0, p2);
  52. res = min(res, e1 * e2);
  53. }
  54. }
  55. }
  56. }
  57. return res < 40005 * 40005? res : 0;
  58. }
  59.  
  60. private:
  61. bool isPerpen(const Dot& p0, const Dot& p1, const Dot& p2)
  62. {
  63. Dot vector1(p1.x - p0.x, p1.y - p0.y), vector2(p2.x - p0.x, p2.y - p0.y);
  64. return vector1.x * vector2.x + vector1.y * vector2.y == 0;
  65. }
  66.  
  67. double getLen(const Dot& p0, const Dot& p1)
  68. {
  69. return sqrt(pow(p1.x - p0.x, 2) + pow(p1.y - p0.y, 2));
  70. }
  71. };
Add Comment
Please, Sign In to add comment