593. Valid Square

Problem statement:

Given the coordinates of four points in 2D space, return whether the four points could construct a square.

The coordinate (x,y) of a point is represented by an integer array with two integers.

Example:

Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: True

Note:

  1. All the input integers are in the range [-10000, 10000].
  2. A valid square has four equal sides with positive length and four equal angles (90-degree angles).
  3. Input points have no order.

Solution one: a set to find the relative of four points(AC).

As the second problem in this contest, the key points are to find the right order of these four points and check the if one/two/three/fours points are overlapped. It will be very easier if we know the relative positions of these four points. The answer comes out to check the length of four sides and two diagonals.

I use the default sorting characteristic of a set instead of designing an algorithm to find their relative positions.  

The final order after sorting by a set is 0, 1, 3, 2 in counterclockwise.

The efficiency does not matter in this problem, correctness is on the top.

class Solution {
public:
    bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
        vector<vector<int>> points(4, vector<int>(2, 0));
        set<vector<int>> points_set;
        points_set.insert(p1);
        points_set.insert(p2);
        points_set.insert(p3);
        points_set.insert(p4);
        if(points_set.size() != 4){
            return false;
        }
        int idx = 0;
        for(auto point : points_set){
            points[idx] = point;
            idx++;
        }
        if(    dis_square(points[0], points[1]) == dis_square(points[1], points[3]) 
            && dis_square(points[1], points[3]) == dis_square(points[3], points[2]) 
            && dis_square(points[3], points[2]) == dis_square(points[2], points[0]) 
            && dis_square(points[2], points[0]) == dis_square(points[0], points[1]) 
            && dis_square(points[0], points[3]) == dis_square(points[1], points[2])){
            return true;   
        }
        return false;
    }
private:
    int dis_square(vector<int> p1, vector<int> p2){
        return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
    }
};

Solution two: STL sort algorithm(AC). This is concise and easy to understand(Better).

Another good alternative approach to find the relative position of these four points are the sort algorithm in STL.

The default sorting algorithm sort the first element and sort the second element. The sorted order is still 0, 1, 3, 2 in counterclockwise. 

But, we need to check if one or more positions are overlapped before sorting.

class Solution {
public:
    bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
        // check if one or more points are overlapped
        if(p1 == p2 || p1 == p3 || p1 == p4 || p2 == p3 || p2 == p4 || p3 == p4){
            return false;
        }
        vector<vector<int>> points = {p1, p2, p3, p4};
        sort(points.begin(), points.end());
        if(dis_square(points[0], points[1]) == dis_square(points[1], points[3]) // check four sides
        && dis_square(points[1], points[3]) == dis_square(points[3], points[2])
        && dis_square(points[3], points[2]) == dis_square(points[2], points[0])
        && dis_square(points[2], points[0]) == dis_square(points[0], points[1])
        // check the diagonals
        && dis_square(points[0], points[3]) == dis_square(points[1], points[2])){
            return true;
        }
        return false;
    }
private:
    int dis_square(vector<int> p1, vector<int> p2){
        return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
    }
};

 

posted @ 2017-05-22 03:32  蓝色地中海  阅读(196)  评论(0编辑  收藏  举报