题目
代码
package fushi;
import java.util.Scanner;
public class test2 {
static int[][] point;//坐标
static int[] result = new int[5];//表示分别得分为0、1、2、3、4的回收站选址个数
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String t = scan.nextLine();
int num = Integer.parseInt(t);
point = new int[num][2];
for (int i = 0; i < num; i++) {
String s = scan.nextLine();
String[] ss = s.split(" ");
point[i][0] = Integer.parseInt(ss[0]);//坐标x
point[i][1] = Integer.parseInt(ss[1]);//坐标y
// point[i][3] = 1; 不用,因为输入的坐标都是有垃圾的
}
//判断是否上下左右是否存在垃圾
judge(num);
for (int i = 0; i < 5; i++) {
System.out.println(result[i]);
}
}
private static void judge(int n) {//查询n个点
for (int i = 0; i < n; i++) {
//上 下 左 右
int num=0;//当num=4说明上下左右都有垃圾
int key=0;//key为对角
for (int j = 0; j < n; j++) {
if (point[i][0]==point[j][0]&&point[i][1]+1==point[j][1]) {//上方向 x一样 y差1
num++;
}
if (point[i][0]==point[j][0]&&point[i][1]-1==point[j][1]) {//下方向 x一样 y差1
num++;
}
if (point[i][0]-1==point[j][0]&&point[i][1]==point[j][1]) {//左方向 y一样 x差1
num++;
}
if (point[i][0]+1==point[j][0]&&point[i][1]==point[j][1]) {//右方向 y一样 x差1
num++;
}
}
if(num==4) {//说明该点上下左右都有垃圾 现在开始判断对角位置是否有垃圾来判分
System.out.println("上下左右都有垃圾:"+point[i][0]+","+point[i][1]);
for (int j = 0; j < n; j++) {
//上右对角 x多1 y多1
if (point[i][0]+1==point[j][0]&&point[i][1]+1==point[j][1]) {
key++;
}
//下右对角 x多1 y差1
if (point[i][0]+1==point[j][0]&&point[i][1]-1==point[j][1]) {
key++;
}
//下左对角 x差1 y差1
if (point[i][0]-1==point[j][0]&&point[i][1]-1==point[j][1]) {
key++;
}
//上左对角 x差1 y多1
if (point[i][0]-1==point[j][0]&&point[i][1]+1==point[j][1]) {
key++;
}
}
result[key]++;//例如key最终为3,则result[3]就为得分为3的回收站个数
}
}
}
}