ในปัญหานี้ เราได้รับ N คะแนนที่อยู่ในระนาบ 2 มิติ งานของเราคือ หาจำนวนจุดที่มีอย่างน้อย 1 จุด ด้านบน ด้านล่าง ซ้ายหรือขวา .
เราต้องนับคะแนนทั้งหมดที่มีอย่างน้อย 1 คะแนนซึ่งเป็นไปตามเงื่อนไขด้านล่าง
ชี้ด้านบน − จุดจะมีพิกัด X เหมือนกัน และพิกัด Y จะมากกว่าค่าปัจจุบันหนึ่งจุด
ชี้ด้านล่าง − จุดจะมีพิกัด X เหมือนกัน และพิกัด Y จะน้อยกว่าค่าปัจจุบันหนึ่งจุด
ชี้ไปทางซ้าย − จุดจะมีพิกัด Y เหมือนกัน และพิกัด X จะน้อยกว่าค่าปัจจุบันหนึ่งจุด
ชี้ไปทางขวา − จุดจะมีพิกัด Y เหมือนกัน และพิกัด X จะมากกว่าค่าปัจจุบันหนึ่งจุด
มาดูตัวอย่างเพื่อทำความเข้าใจปัญหากัน
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