📌  相关文章
📜  找到不属于任何正方形的坐标

📅  最后修改于: 2021-10-23 08:27:26             🧑  作者: Mango

给定一个数组arr[](4 * N + 1)对坐标组成,表示任何N 个正方形的角的坐标,这样只有一个坐标不属于任何正方形,任务是找到不属于任何正方形的坐标不属于任何广场。

例子:

方法:根据以下观察可以解决给定的问题:

  • 由于N ≥ 2 ,正方形边的坐标将至少出现两次。因此,由于只有一个点不在边界上,因此至少出现两次的 x 坐标之间的最大值将为我们提供正方形右侧的 x 坐标。
  • 其他三个边可以类似地通过最大值/最小值和 x-/y 坐标的不同组合获得。
  • 知道正方形的边后,很容易识别不在边界上的点。

请按照以下步骤解决问题:

  • 使用变量i在范围[0, N] 上迭代并执行以下步骤:
    • 初始化变量X1,Y12E9X2,Y2-2e9存储方的上部和下部边界的点。
    • 使用变量j在范围[0, N] 上迭代并执行以下步骤:
      • 如果i不等于j ,则执行以下步骤:
        • x1设置为x1p[j].first的最小值。
        • x2设置为x2p[j].first的最大值。
        • y1设置为y1p[j].second 的最小值
        • y2设置为y2p[j].second 的最小值
    • 初始化一个变量,说ok,变量cnt1, cnt2, cnt3, cnt40以存储具有最大值和最小值的点数为x1, x2, y1, y2
    • 使用变量j在范围[0, N] 上迭代并执行以下步骤:
      • 如果i不等于j ,则执行以下步骤:
        • 如果p[j].first等于x1,则将cnt1的值增加1
        • 如果p[j].first等于x2,则将cnt2的值增加1
        • 如果p[j].second等于y1,则将cnt3的值增加1
        • 如果p[j].second等于y2,则将cnt4的值增加1
      • 否则,将ok的值设置为false
    • 如果ok的值为真,并且cnt1、cnt2、cnt3、cnt4的值大于等于N,x2-x2等于y2-y1 ,那么p[i]就是需要的点。打印答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
typedef long long ll;
#define fi first
#define se second
 
// Function to find the point that is
// not a part of the side of a square
void findPoint(int n,
               vector > p)
{
    // Traverse each pair of coordinates
    for (int i = 0; i < n * 4 + 1; ++i) {
 
        int x1 = 2e9, x2 = -2e9;
        int y1 = 2e9, y2 = -2e9;
 
        for (int j = 0; j < n * 4 + 1; ++j)
            if (i != j) {
 
                // Minimize x-coordinate
                // in all the points
                // except current point
                x1 = min(x1, p[j].fi);
 
                // Maximize x-coordinate in
                // all the points except
                // the current point
                x2 = max(x2, p[j].fi);
 
                // Minimize y-coordinate
                // in all the points
                // except current point
                y1 = min(y1, p[j].se);
 
                // Maximize y-coordinate
                // in all the points
                // except current point
                y2 = max(y2, p[j].se);
            }
 
        bool ok = 1;
 
        int c1 = 0, c2 = 0;
        int c3 = 0, c4 = 0;
 
        for (int j = 1; j <= n * 4 + 1; ++j)
            if (i != j) {
 
                // If x-coordinate matches
                // with other same line
                if ((p[j].fi == x1
                     || p[j].fi == x2)
                    || ((p[j].se == y1
                         || p[j].se == y2))) {
 
                    if (p[j].fi == x1)
                        ++c1;
 
                    if (p[j].fi == x2)
                        ++c2;
 
                    // If y coordinate matches
                    // with other same line
                    if (p[j].se == y1)
                        ++c3;
 
                    if (p[j].se == y2)
                        ++c4;
                }
                else
                    ok = 0;
            }
 
        // Check if the condition
        // for square exists or not
        if (ok && c1 >= n && c2 >= n
            && c3 >= n && c4 >= n
            && x2 - x1 == y2 - y1) {
 
            // Print the output
            cout << p[i].fi << " "
                 << p[i].se << "\n";
        }
    }
}
 
// Driver Code
int main()
{
    int N = 2;
    vector > arr
        = { { 0, 0 }, { 0, 1 }, { 0, 2 },
            { 1, 0 }, { 1, 1 }, { 1, 2 },
            { 2, 0 }, { 2, 1 }, { 2, 2 } };
 
    findPoint(N, arr);
 
    return 0;
}


Java
import java.util.ArrayList;
 
//Java program for above approach
class GFG{
    static class pair{
        T fi;
        V se;
        pair(T a, V b){
            this.fi = a;
            this.se = b;
        }
    }
 
    // Function to find the point that is
    // not a part of the side of a square
    static void findPoint(int n,
                   ArrayList > p)
    {
        // Traverse each pair of coordinates
        for (int i = 0; i < n * 4 + 1; ++i) {
 
            int x1 = (int) 2e9, x2 = (int) -2e9;
            int y1 = (int) 2e9, y2 = (int) -2e9;
 
            for (int j = 0; j < n * 4 + 1; ++j)
                if (i != j) {
 
                    // Minimize x-coordinate
                    // in all the points
                    // except current point
                    x1 = Math.min(x1, p.get(j).fi);
 
                    // Maximize x-coordinate in
                    // all the points except
                    // the current point
                    x2 = Math.max(x2, p.get(j).fi);
 
                    // Minimize y-coordinate
                    // in all the points
                    // except current point
                    y1 = Math.min(y1, p.get(j).se);
 
                    // Maximize y-coordinate
                    // in all the points
                    // except current point
                    y2 = Math.max(y2, p.get(j).se);
                }
 
            boolean ok = true;
 
            int c1 = 0, c2 = 0;
            int c3 = 0, c4 = 0;
 
            for (int j = 1; j < n * 4 + 1; ++j)
                if (i != j) {
 
                    // If x-coordinate matches
                    // with other same line
                    if ((p.get(j).fi == x1
                            || p.get(j).fi == x2)
                            || ((p.get(j).se == y1
                            || p.get(j).se == y2))) {
 
                        if (p.get(j).fi == x1)
                            ++c1;
 
                        if (p.get(j).fi == x2)
                            ++c2;
 
                        // If y coordinate matches
                        // with other same line
                        if (p.get(j).se == y1)
                            ++c3;
 
                        if (p.get(j).se == y2)
                            ++c4;
                    }
                    else
                        ok = false;
                }
 
            // Check if the condition
            // for square exists or not
            if (ok && c1 >= n && c2 >= n
                    && c3 >= n && c4 >= n
                    && x2 - x1 == y2 - y1) {
 
                // Print the output
                System.out.println(p.get(i).fi + " " + p.get(i).se);
 
            }
        }
    }
 
    //Driver Code
    public static void main(String[] args) {
        int N = 2;
        ArrayList > arr = new ArrayList<>();
        arr.add(new pair(0,0));
        arr.add(new pair(0,1));
        arr.add(new pair(0,2));
        arr.add(new pair(1,0));
        arr.add(new pair(1,1));
        arr.add(new pair(1,2));
        arr.add(new pair(2,0));
        arr.add(new pair(2,1));
        arr.add(new pair(2,2));
        findPoint(N, arr);
 
    }
}
 
// This code is contributed by hritikrommie.


Python3
# Python 3 program for the above approach
 
# Function to find the point that is
# not a part of the side of a square
def findPoint(n, p):
    # Traverse each pair of coordinates
    for i in range(n * 4 + 1):
        x1 = 2e9
        x2 = -2e9
        y1 = 2e9
        y2 = -2e9
 
        for j in range(n * 4 + 1):
            if (i != j):
 
                # Minimize x-coordinate
                # in all the points
                # except current point
                x1 = min(x1, p[j][0])
 
                # Maximize x-coordinate in
                # all the points except
                # the current point
                x2 = max(x2, p[j][0])
 
                # Minimize y-coordinate
                # in all the points
                # except current point
                y1 = min(y1, p[j][1])
 
                # Maximize y-coordinate
                # in all the points
                # except current point
                y2 = max(y2, p[j][1])
 
        ok = 1
 
        c1 = 0
        c2 = 0
        c3 = 0
        c4 = 0
 
        for j in range(1,n * 4 + 1,1):
            if (i != j):
 
                # If x-coordinate matches
                # with other same line
                if ((p[j][0] == x1 or p[j][0] == x2) or (p[j][1] == y1 or p[j][1] == y2)):
                    if (p[j][0] == x1):
                        c1 += 1
 
                    if (p[j][0] == x2):
                        c2 += 1
 
                    # If y coordinate matches
                    # with other same line
                    if (p[j][1] == y1):
                        c3 += 1
 
                    if (p[j][1] == y2):
                        c4 += 1
                else:
                    ok = 0
 
        # Check if the condition
        # for square exists or not
        if (ok and c1 >= n and c2 >= n and c3 >= n and c4 >= n and x2 - x1 == y2 - y1):
 
            # Print the output
            print(p[i][0],p[i][1])
         
# Driver Code
if __name__ == '__main__':
    N = 2
    arr =  [[0, 0],[0, 1],[0, 2],[1, 0],[1, 1],[1, 2],[2, 0],[2, 1],[2, 2]]
 
    findPoint(N, arr)
     
    # This code is contributed by ipg2016107.


输出:
1 1

时间复杂度: O(N 2 )
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程