蓝桥杯-平面4点最小距离


已知平面上若干个点的坐标。

需要求出在所有的组合中,4个点间平均距离的最小值(四舍五入,保留2位小数)。

比如有4个点:a,b,c,d, 则平均距离是指:ab, ac, ad, bc, bd, cd 这6个距离的平均值。

每个点的坐标表示为:横坐标,纵坐标

坐标的取值范围是:1~1000

例如,如果程序输入:
10,10
20,20
80,50
10,20
20,10

则程序应该输出:
11.38

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;


public class MinDistance {

    /**
     * @param args
     */
    
    private ArrayList<Point> list = new ArrayList<Point>();
    private HashMap<String,Double> map = new HashMap<String,Double>();
    private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
    public void input() throws NumberFormatException, IOException{
        String str = null;
        while(!(str=in.readLine()).equals("")){
            String[] strs = str.split(",");
            int x = Integer.parseInt(strs[0]);
            int y = Integer.parseInt(strs[1]);
            Point p = new Point(x,y);
            list.add(p);
        }
        System.out.println(list.size());
        /*
        for(Point p:list){
            System.out.println(p.x+","+p.y);
        }
        */
    }
    
    public void process(){
        int[] ind = null;
        double min = Double.MAX_VALUE;
        for(int i=0;i<list.size();i++){
            int[] indexs = new int[4];
            indexs[0] = i;
            double distance = 0;
            for(int j=1;j<4;j++){
                double d = Double.MAX_VALUE;
                for(int k=0;k<list.size();k++){
                    boolean b = true;
                    for(int m=0;m<j;m++){
                        if(k==indexs[m]){
                            b=false;
                            break;
                        }
                    }
                    if(b){
                        Double sum = 0.0;
                        for(int m=0;m<j;m++){
                            String s1 = indexs[m]+""+k;
                            String s2 = k+""+indexs[m];
                            Double num = null;
                            if((num=map.get(s1))==null&&(num=map.get(s2))==null){
                                num = Math.pow(list.get(k).x-list.get(indexs[m]).x, 2)+Math.pow(list.get(k).y-list.get(indexs[m]).y, 2);
                                map.put(s1, num);
                            }
                            sum+=num;
                        }
                        if(sum<d){
                            d=sum;
                            indexs[j]=k;
                        }
                    }
                }
                distance+=d;
            }
            if(distance<min){
                 min = distance;
                 ind = indexs;
            }
        }
        double dis = 0;
        for(int i=0;i<4;i++){
            for(int j=i+1;j<4;j++){
                String s1 = ind[i]+""+ind[j];
                String s2 = ind[j]+""+ind[i];
                Double dou = map.get(s1);
                if(dou==null)
                    dou=map.get(s2);
                dis+=Math.sqrt(dou);
            }
        }
        System.out.printf("%.2f", dis/6.0);
    }
    
    public static void main(String[] args) throws NumberFormatException, IOException {
        // TODO Auto-generated method stub
        MinDistance md = new MinDistance();
        md.input();
        md.process();

    }
     class Point{
        int x=0;
        int y=0;
        
        public Point(int x,int y){
            this.x = x;
            this.y = y;
        }
    }
}


转载于:https://my.oschina.net/u/1048681/blog/263080

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值