CCF201912-2 回收站选址

最直接的解法是将所有的坐标点都标记到一个二维数组里,然后一个个坐标点遍历判断。但是坐标的输入范围为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;
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值