最直接的解法是将所有的坐标点都标记到一个二维数组里,然后一个个坐标点遍历判断。但是坐标的输入范围为0-10的9次方,而n<=1000,因此暴力破解不太可行。
之后考虑每个坐标点分别记录自己相邻坐标点的数量,确定是否能够成为回收站,并且记录得分。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
List<Point> points = new ArrayList<>();
int[] garbageStationScores = new int[5];
for (int i = 0; i < n; i++) {
int x = input.nextInt();
int y = input.nextInt();
points.add(new Point(x, y));
}
for (int i = 0; i < points.size(); i++) {
for (int j = 0; j < points.size(); j++) {
points.get(j).checkIsNearby(points.get(i));
}
}
Point point = null;
for (int i = 0; i < points.size(); i++) {
point = points.get(i);
if (point.isGarbageStation) {
garbageStationScores[point.socre]++;
}
}
for (int i = 0; i < garbageStationScores.length; i++) {
System.out.println(garbageStationScores[i]);
}
}
static class Point {
int x;
int y;
int nearCount = 0;
int socre = 0;
boolean isGarbageStation = false;
void checkIsNearby(Point p) {
if (p.x == x && p.y == y) {
return;
}
int distance = (int) (Math.pow(p.x-x, 2) + Math.pow(p.y-y, 2));
if (distance == 1) {
nearCount += 1;
if (nearCount == 4) {
isGarbageStation = true;
}
} else if (distance == 2) {
socre += 1;
}
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
}