
在这个问题中,我们得到了位于 2D 平面上的 N 个点。我们的任务是找到其上方、下方、左侧或右侧至少有 1 个点的点的数量。
我们需要计算所有至少有 1 个点的点1 个满足以下任一条件的点。
其上方的点− 该点将具有相同的 X 坐标,并且 Y 坐标比其当前值大 1。 p>
其下方的点− 该点将具有相同的 X 坐标,且 Y 坐标比其当前值小 1。
其左侧的点− 该点将具有相同的 Y 坐标,并且 X 坐标比其当前值小 1。
立即学习“C++免费学习笔记(深入)”;
该点右侧的点 − 该点将具有相同的Y坐标和X坐标比当前值大1。
让我们举个例子来理解这个问题,
Input : arr[] = {{1, 1}, {1, 0}, {0, 1}, {1, 2}, {2, 1}}
Output :1为了解决这个问题,我们需要从平面上取出每个点,并找到其相邻点可以具有的 X 和 Y 坐标的最大值和最小值,以进行有效计数。如果存在任何具有相同 X 坐标且 Y 值在该范围内的坐标。我们将增加点数。我们将计数存储在变量中并返回它。
让我们举个例子来理解问题
#include <bits/stdc++.h>
using namespace std;
#define MX 2001
#define OFF 1000
struct point {
int x, y;
};
int findPointCount(int n, struct point points[]){
int minX[MX];
int minY[MX];
int maxX[MX] = { 0 };
int maxY[MX] = { 0 };
int xCoor, yCoor;
fill(minX, minX + MX, INT_MAX);
fill(minY, minY + MX, INT_MAX);
for (int i = 0; i < n; i++) {
points[i].x += OFF;
points[i].y += OFF;
xCoor = points[i].x;
yCoor = points[i].y;
minX[yCoor] = min(minX[yCoor], xCoor);
maxX[yCoor] = max(maxX[yCoor], xCoor);
minY[xCoor] = min(minY[xCoor], yCoor);
maxY[xCoor] = max(maxY[xCoor], yCoor);
}
int pointCount = 0;
for (int i = 0; i < n; i++) {
xCoor = points[i].x;
yCoor = points[i].y;
if (xCoor > minX[yCoor] && xCoor < maxX[yCoor])
if (yCoor > minY[xCoor] && yCoor < maxY[xCoor])
pointCount++;
}
return pointCount;
}
int main(){
struct point points[] = {{1, 1}, {1, 0}, {0, 1}, {1, 2}, {2, 1}};
int n = sizeof(points) / sizeof(points[0]);
cout<<"The number of points that have atleast one point above, below, left, right is "<<findPointCount(n, points);
}The number of points that have atleast one point above, below, left, right is 1
以上就是找到在C++中至少有一个点在其上方、下方、左方或右方的点的数量的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号