欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

CSP 201912-2 回收站选址 Java 满分

程序员文章站 2022-04-16 16:02:01
import java.util.*;public class Main { public static void main(String[] args) { LinkedHashSet points = new LinkedHashSet(); //存储输入的垃圾点 HashSet condition1 = new HashSet(); //存......

CSP 201912-2 回收站选址 Java 满分
CSP 201912-2 回收站选址 Java 满分
CSP 201912-2 回收站选址 Java 满分
CSP 201912-2 回收站选址 Java 满分

import java.util.*;

public class Main {
    public static void main(String[] args) {
        LinkedHashSet<Point> points = new LinkedHashSet<Point>();   //存储输入的垃圾点
        HashSet<Point> condition1 = new HashSet<Point>();   //存储某个垃圾点上下左右位置垃圾点
        HashSet<Point> condition2 = new HashSet<Point>();   //存储某个垃圾点四个对焦位置垃圾点
        int[] num = new int[5];         //存储得分的数组
        int flag = 0;
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        //输入垃圾点,并存入points中
        for (int i = 0; i < n; i++) {
            int tempX = sc.nextInt();
            int tempY = sc.nextInt();
            points.add(new Point(tempX, tempY));
        }
        //创建垃圾点的迭代器
        Iterator<Point> iterator = points.iterator();
        while (iterator.hasNext()) {
            flag = 0;//表示得分数
            //迭代每个垃圾点时,重置存储此垃圾点周围垃圾点的Set
            condition1.clear();
            condition2.clear();
            Point p = iterator.next();
            //创建此垃圾点上下左右位置的垃圾点,并存入condition1中
            condition1.add(new Point(p.x + 1, p.y));
            condition1.add(new Point(p.x - 1, p.y));
            condition1.add(new Point(p.x, p.y + 1));
            condition1.add(new Point(p.x, p.y - 1));
            //判断此垃圾点是否包含上下左右垃圾点
            if (points.containsAll(condition1)) {
                //创建此垃圾点四个对角位置的垃圾点,并存入condition2中
                condition2.add(new Point(p.x + 1, p.y + 1));
                condition2.add(new Point(p.x - 1, p.y - 1));
                condition2.add(new Point(p.x - 1, p.y + 1));
                condition2.add(new Point(p.x + 1, p.y - 1));
                Iterator<Point> iteratorCon = condition2.iterator();
                while (iteratorCon.hasNext()) {
                    Point point = iteratorCon.next();
                    if (points.contains(point)) {
                        flag++;
                    }
                }
            } else {
                continue;
            }
            num[flag]++;
        }
        for (int i = 0; i < 5; i++) {
            System.out.println(num[i]);
        }
    }
}

class Point {
    int x;
    int y;

    Point(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;
        Point point = (Point) o;
        return x == point.x &&
                y == point.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }
}

本文地址:https://blog.csdn.net/Mrwyx/article/details/110495857