分析
把x,y坐标拼接成一个字符串(x,y)作为Set的key,保存到Set中,遍历Set,取出坐标,然后判断上下左右四个点是否在Set中,如果在,进而判断,四个角是否在Set中,用一个数组,保存回收站的分数,key是(0-4),值是选址个数。
import java.util.*;
public class RecycleBin {
public static class Pos {
int x;
int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Pos(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pos pos = (Pos) o;
return x == pos.x && y == pos.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
Set<Pos> set = new HashSet<>();
int[] arr = new int[5];
for(int i = 0; i < num; i++){
int x = scanner.nextInt();
int y = scanner.nextInt();
Pos pos = new Pos(x,y);
set.add(pos);
}
for(Pos pos : set){
int x = pos.getX();
int y = pos.getY();
Pos pUp = new Pos(x,y+1);
Pos pDown = new Pos(x,y-1);
Pos pLeft = new Pos(x+1,y);
Pos pRight = new Pos(x-1,y);
Pos pRightUp = new Pos(x+1,y+1);
Pos pRightDown = new Pos(x+1,y-1);
Pos pLeftUp = new Pos(x-1,y+1);
Pos pLeftDown = new Pos(x-1,y-1);
if(set.contains(pUp)&&set.contains(pDown)
&&set.contains(pLeft) &&set.contains(pRight)){
int count = 0;
if(set.contains(pRightUp)){
count++;
}
if(set.contains(pRightDown)){
count++;
}
if(set.contains(pLeftUp)){
count++;
}
if(set.contains(pLeftDown)){
count++;
}
arr[count]++;
}
}
for(int i : arr){
System.out.println(i);
}
}
}