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

Posted xcxfury

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于java学习(10):求最接近点对问题的主要内容,如果未能解决你的问题,请参考以下文章

平面点集最接近点对问题怎么做

分治法最接近点对问题(转)

一维最接近点对

逆序问题,最大间隙问题,棋盘覆盖问题,最接近点对问题

平面最接近点对

java SpringRetry学习的代码片段