• java学习(10):求最接近点对问题


      1 import java.util.ArrayList;
      2 import java.util.Collections;
      3 import java.util.List;
      4 import java.util.Scanner;
      5 
      6 public class Main {
      7 
      8     public static void main(String[] args) {
      9         Scanner scanner = new Scanner(System.in);
     10         int N = -1;
     11         while (N != 0) {
     12             N = scanner.nextInt();
     13             List<Point> points = new ArrayList<>();
     14             for (int i = 0; i < N; i++) {
     15                 Point point = new Point();
     16                 point.setX(scanner.nextDouble());
     17                 point.setY(scanner.nextDouble());
     18                 points.add(point);
     19             }
     20             if(N!=0){
     21                 double min = MinDistance(points);
     22                 System.out.printf("%.2f", min);
     23             }    
     24         }
     25     }
     26 
     27     public static double MinDistance(List<Point> points) {
     28         int n = points.size();
     29         if (n < 2)
     30             return Integer.MAX_VALUE;
     31         if (n == 2) {
     32             double distance = Distance(points.get(0), points.get(1));
     33             return distance;
     34         }
     35         Collections.sort(points);// 将点按照x坐标排好
     36         // 分界线为中间两点x坐标的一半
     37         double m = (points.get(points.size() / 2 - 1).getX() + points.get(points.size() / 2).getX()) / 2;
     38         // 以x = m 为界限分为两个点集
     39         List<Point> leftPoints = new ArrayList<>();
     40         List<Point> rightPoints = new ArrayList<>();
     41         leftPoints.addAll(points.subList(0, points.size() / 2));
     42         rightPoints.addAll(points.subList(points.size() / 2, points.size()));
     43         // 得到左右两个点集的最短距离
     44         double leftMin = MinDistance(leftPoints);
     45         double rightMin = MinDistance(rightPoints);
     46 
     47         // 得到最短距离
     48         double min = Math.min(leftMin, rightMin);
     49 
     50         // 创建P1点集和P2点集
     51         List<Point> P1 = new ArrayList<>();
     52         List<Point> P2 = new ArrayList<>();
     53         for (Point point : points) {
     54             if (point.getX() >= (m - min) && point.getX() < m) {
     55                 P1.add(point);
     56             }
     57             if (point.getX() > m && point.getX() <= m + min) {
     58                 P2.add(point);
     59             }
     60         }
     61 
     62         double min2 = Integer.MAX_VALUE;
     63         double distance;
     64         boolean flag1, flag2;
     65         if (P1 != null && P2 != null) {
     66             for (Point point : P1) {
     67                 for (Point point2 : P2) {
     68                     flag1 = (point2.getY() >= (point.getY() - min) && point2.getY() <= point.getY());
     69                     flag2 = (point2.getY() >= point.getY() && point2.getY() <= (point.getY() + min));
     70                     if (flag1 || flag2) {
     71                         distance = Distance(point,point2);
     72                         if (distance < min2) {
     73                             min2 = distance;
     74                         }
     75 
     76                     }
     77                 }
     78             }
     79             return Math.min(min, min2);
     80         } else {
     81             return min;
     82         }
     83     }
     84     
     85     public static double Distance(Point point1,Point point2){
     86         return Math.sqrt((point1.getX() - point2.getX()) * (point1.getX() - point2.getX())
     87                 + (point1.getY() - point2.getY()) * (point1.getY() - point2.getY()));
     88     }
     89 }
     90 
     91 class Point implements Comparable<Point> {
     92     // 点的x.y坐标
     93     private double x;
     94     private double y;
     95 
     96     public void setX(double x) {
     97         this.x = x;
     98     }
     99 
    100     public void setY(double y) {
    101         this.y = y;
    102     }
    103 
    104     public double getX() {
    105         return x;
    106     }
    107 
    108     public double getY() {
    109         return y;
    110     }
    111 
    112     @Override
    113     public String toString() {
    114         return "("+x+","+y+")";
    115     }
    116     @Override
    117     public int compareTo(Point o) {
    118         if (x > o.getX())
    119             return 1;
    120         else if (x == o.getX()) {
    121             return 0;
    122         } else {
    123             return -1;
    124         }
    125     }
    126 }
  • 相关阅读:
    20145328 《信息安全系统设计基础》第6周学习总结
    20145328 《信息安全系统设计基础》第5周学习总结
    2017-2018-2 《网络对抗技术》 20155322 第二周 Exp1 PC平台逆向破解(5)M
    2017-2018-1 《信息安全系统设计基础》 20155322 十六周 课上实践
    2017-2018-1 《信息安全系统设计基础》 20155322 十六周 课下实践
    20155322 2017-2018-1《信息安全系统设计基础》课程总结
    20155322 2017-2018-1《信息安全系统设计基础》第十四周学习总结
    20155322 2017-2018-1 《信息安全系统设计基础》 第十三周学习总结
    20155322 2017-2018-1《信息安全系统设计基础》实验五-通信协议设计
    20155322 2017-2018-1《信息安全系统设计基础》实验四-外设驱动程序设计
  • 原文地址:https://www.cnblogs.com/xcxfuryit/p/7380493.html
Copyright © 2020-2023  润新知