给定“ n”对点,任务是找到四个点,使它们形成一个正方形,其边平行于x和y轴,否则打印“无此正方形”。
如果可以使用一个以上的正方形,则选择面积最大的正方形。
例子:
Input : n = 6, points = (1, 1), (4, 4), (3, 4), (4, 3), (1, 4), (4, 1)
Output :
side of the square is : 3, points of the square are 1, 1 4, 1 1, 4 4, 4
Explanation: The points 1, 1 4, 1 1, 4 4, 4 form a square of side 3
Input :n= 6, points= (1, 1), (4, 5), (3, 4), (4, 3), (7, 4), (3, 1)
Output :No such square
简单方法:选择具有四个嵌套循环的所有可能的点对,然后查看这些点是否形成与主轴平行的正方形。如果是,则检查该区域是否是到目前为止最大的正方形,并存储结果,然后在程序末尾显示结果。
时间复杂度: O(N ^ 4)
高效方法:为正方形的右上角和左下角创建一个嵌套循环,并用这两个点组成一个正方形,然后检查假定的其他两个点是否确实存在。要检查点是否存在,请创建地图并将点存储在地图中,以减少检查点是否存在的时间。另外,请检查到目前为止最大面积的正方形,并在最后打印。
下面是上述方法的实现:
CPP
// C++ implemenataion of the above approach
#include
using namespace std;
// find the largest square
void findLargestSquare(long long int points[][2], int n)
{
// map to store which points exist
map, int> m;
// mark the available points
for (int i = 0; i < n; i++) {
m[make_pair(points[i][0], points[i][1])]++;
}
long long int side = -1, x = -1, y = -1;
// a nested loop to choose the opposite corners of square
for (int i = 0; i < n; i++) {
// remove the chosen point
m[make_pair(points[i][0], points[i][1])]--;
for (int j = 0; j < n; j++) {
// remove the chosen point
m[make_pair(points[j][0], points[j][1])]--;
// check if the other two points exist
if (i != j
&& (points[i][0]-points[j][0]) == (points[i][1]-points[j][1])){
if (m[make_pair(points[i][0], points[j][1])] > 0
&& m[make_pair(points[j][0], points[i][1])] > 0) {
// if the square is largest then store it
if (side < abs(points[i][0] - points[j][0])
|| (side == abs(points[i][0] - points[j][0])
&& ((points[i][0] * points[i][0]
+ points[i][1] * points[i][1])
< (x * x + y * y)))) {
x = points[i][0];
y = points[i][1];
side = abs(points[i][0] - points[j][0]);
}
}
}
// add the removed point
m[make_pair(points[j][0], points[j][1])]++;
}
// add the removed point
m[make_pair(points[i][0], points[i][1])]++;
}
// display the largest square
if (side != -1)
cout << "Side of the square is : " << side
<< ", \npoints of the square are " << x << ", " << y
<< " "
<< (x + side) << ", " << y
<< " "
<< (x) << ", " << (y + side)
<< " "
<< (x + side) << ", " << (y + side) << endl;
else
cout << "No such square" << endl;
}
//Driver code
int main()
{
int n = 6;
// given points
long long int points[n][2]
= { { 1, 1 }, { 4, 4 }, { 3, 4 }, { 4, 3 }, { 1, 4 }, { 4, 1 } };
// find the largest square
findLargestSquare(points, n);
return 0;
}
Python3
# Python3 implemenataion of the above approach
# find the largest square
def findLargestSquare(points,n):
# map to store which points exist
m = dict()
# mark the available points
for i in range(n):
m[(points[i][0], points[i][1])] = \
m.get((points[i][0], points[i][1]), 0) + 1
side = -1
x = -1
y = -1
# a nested loop to choose the opposite corners of square
for i in range(n):
# remove the chosen point
m[(points[i][0], points[i][1])]-=1
for j in range(n):
# remove the chosen point
m[(points[j][0], points[j][1])]-=1
# check if the other two points exist
if (i != j and (points[i][0]-points[j][0]) == \
(points[i][1]-points[j][1])):
if (m[(points[i][0], points[j][1])] > 0 and
m[(points[j][0], points[i][1])] > 0):
# if the square is largest then store it
if (side < abs(points[i][0] - points[j][0])
or (side == abs(points[i][0] - points[j][0])
and ((points[i][0] * points[i][0]
+ points[i][1] * points[i][1])
< (x * x + y * y)))):
x = points[i][0]
y = points[i][1]
side = abs(points[i][0] - points[j][0])
# add the removed point
m[(points[j][0], points[j][1])] += 1
# add the removed point
m[(points[i][0], points[i][1])] += 1
# display the largest square
if (side != -1):
print("Side of the square is : ",side
,", \npoints of the square are ",x,", ",y
," "
,(x + side),", ",y
," "
,(x),", ",(y + side)
," "
,(x + side),", ",(y + side))
else:
print("No such square")
# Driver code
n = 6
# given points
points=[[ 1, 1 ],[ 4, 4 ],[ 3, 4 ],[ 4, 3 ],[ 1, 4 ],[ 4, 1 ] ]
# find the largest square
findLargestSquare(points, n)
# This code is contributed by mohit kumar 29
Side of the square is : 3,
points of the square are 1, 1 4, 1 1, 4 4, 4
时间复杂度: O(N ^ 2)