oj1007 分治法

import java.util.ArrayList;
import java.util.Scanner;

import static java.util.Collections.sort;
//AC
class Pos implements Comparable{
    private double x;
    private double y;

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }
    public Pos(){}

    public Pos(double x, double y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public int compareTo(Object o) {
        if(o instanceof  Pos){
           Pos p = (Pos) o;
            if(this.y > p.y){
                return 1;
            }else if(this.y < p.y){
                return -1;
            }
            return 0;
        }
        return 0;
    }
}
public class Main_1007 {
    public static void main(String[] args) {
        Scanner in = new Scanner((System.in));
        int n = in.nextInt();
        while(n != 0){
            ArrayList<Pos> pos = new ArrayList<>();
            for(int i = 1; i <= n; i++){
                double x = in.nextDouble();
                double y = in.nextDouble();
                pos.add(new Pos(x,y));
            }
            //根据y排序
            sort(pos);
            double min = divide(pos,0,pos.size()-1);
            System.out.println(String.format("%.2f",min));
            n = in.nextInt();
        }
    }
    public static double divide(ArrayList<Pos> pos,int left,int right){
        double min = 0;
        int start = 0,end = 0;
        if(left >= right){
            return Double.MAX_VALUE;
        }
        if(left + 1 == right){
            min = dist(pos.get(left),pos.get(right));
            return min;
        }else{
            int mid = (left + right)/2;
            double lDist = divide(pos,left,mid);
            double rDist = divide(pos,mid+1,right);
            min = lDist < rDist ? lDist : rDist;
            boolean flag = false;
            //得到y在[mid.y-min,mid.y+min]的开始下标和结束下标
            for(int i = left; i <= right;i++){
                if(!flag && (Math.abs(pos.get(mid).getY()-pos.get(i).getY()) < min)){
                    start = i;
                    flag = true;
                }else if(flag && (Math.abs(pos.get(mid).getY()-pos.get(i).getY()) >= min)){
                    end = i;
                    break;
                }
            }
            if(!flag){
                return min;
            }
           else{
               for(int i = start; i < end; i++){
                    for(int j = i + 1; j < end; j++){
                        //满足i,j位置的pos对象的X值的距离小于min
                        if( Math.abs(pos.get(i).getX()-pos.get(j).getX())<min){
                                double result = dist(pos.get(i),pos.get(j));
                                if(result <min){
                                    min = result;
                                }
                        }
                    }
               }
            }
        }
        return min;
    }
    public static double dist(Pos p1,Pos p2){
        return Math.sqrt(Math.pow(p1.getX() - p2.getX(),2) + Math.pow(p1.getY() - p2.getY(),2)) / 2;
    }
}

猜你喜欢

转载自blog.csdn.net/suiyingsuiyi/article/details/89334958
0条评论
添加一条新回复