1、 if (d minDist) minDist=d; index1=i; index2=j; return minDist; 程序:import java.util.*;public class ClosestPair1 public static void main(String args) /* *输入需要比较的点的对数存在变量n中 */ Scanner in=new Scanner(System.in); System.out.println(How many pairs of points to compare?(有多少对点需要比较?); int n=in.nextInt(); int
2、 x=new intn; int y=new intn; *输入这些点的横坐标和纵坐标分别存储在xn和ynPlease enter these points,X-coordinate(请输入这些点,横坐标): for(int i=0;ii+) xi=in.nextInt();Please enter these points,Y-coordinate(请输入这些点,纵坐标): yi=in.nextInt(); double minDist=Double.POSITIVE_INFINITY; double d; int indexI=0; int indexJ=0; *求解最近对距离存于minD
3、ist中 double startTime=System.currentTimeMillis();/startTime n-1; for(int j=i+1;jj+) d=Math.sqrt(xi-xj)*(xi-xj)+(yi-yj)*(yi-yj); if(d minDist) indexI=i; indexJ=j; double endTime=System.currentTimeMillis();/endTime *打印输出最后求出的结果,最近的是哪两个点,以及最近距离和程序用的时间The closest pair is:(+xindexI+,+yindexI+) and (+xind
4、exJ+yindexJ+The closest distance is +minDist);Basic Statements take(基本语句用时) +(endTime-startTime)+ milliseconds!运行:分治算法 描述:可以划一条垂线,把点集分成两半:PL和PR。于是最近点对或者在PL中,或者在PR中,或者PL,PR各有一点。把三种距离情况定义为dL, dR, dC.其中dL, dR可以递归求解,于是问题就变为计算dC。设s=min(dL, dR). 通过观察能得出结论:如果dCs,则只需计算dC。如果dC满足这样的条件,则决定dC的两点必然在分割线的s距离之内,称之为
5、带(strip)否则不可能满足dCs, 于是缩小了需要考虑的点的范围。public class ClosestPair2 *输入这些点的横坐标和纵坐标,存储在点数组Sn中Please enter these points,X-coordinate and Y-coordinate.(请输入这些点,x坐标和y坐标): Point S=new Pointn;/starttime int x=in.nextInt(); int y=in.nextInt(); Si=new Point(x,y);+Si.getX()+Si.getY()+ *求出这点的x坐标的中位数mid int minX=(int)
6、Double.POSITIVE_INFINITY; int maxX=(int)Double.NEGATIVE_INFINITY; if(Si.getX()maxX) maxX=Si.getX(); int mid=(minX+maxX)/2; *以mid为界把S中的点分为两组分别存放在范型数组列表point1和point2中 ArrayList point1=new ArrayList(); ArrayList point2=new ArrayList();=mid) point1.add(Si); else point2.add(Si); *将范型数组列表转换为数组类型S1和S2 Poin
7、t S1=new Pointpoint1.size(); Point S2=new Pointpoint2.size(); point1.toArray(S1); point2.toArray(S2); *将S1和S2中的点按x 坐标升序排列 sortX(S1); sortX(S2); *打印输出排序后S1和S2的点 System.out.print(The points in S1 are: S1.length;+S1i.getX()+S1i.getY()+) System.out.println();The points in S2 are: S2.length;+S2i.getX()+S
8、2i.getY()+ *求S1中点的最近对及其距离并打印输出结果 */ double minDist1=Double.POSITIVE_INFINITY; int indexI1=0; int indexJ1=0; S1.length-1; double d=Math.sqrt(Math.pow(S1i.getX()-S1j.getX(),2)+Math.pow(S1i.getY()-S1j.getY(),2); minDist1) minDist1=d; indexI1=i; indexJ1=j;The closest pair in S1 is: +S1indexI1.getX()+S1i
9、ndexI1.getY()+and(+S1indexJ1.getX()+S1indexJ1.getY()+,and the distance is +minDist1); *求S2中点的最近对及其距离并打印输出结果 double minDist2=Double.POSITIVE_INFINITY; int indexI2=0; int indexJ2=0; S2.length-1; double d=Math.sqrt(Math.pow(S2i.getX()-S2j.getX(),2)+Math.pow(S2i.getY()-S2j.getY(),2); minDist2) minDist2=
10、d; indexI2=i; indexJ2=j;The closest pair in S2 is:+S2indexI2.getX()+S2indexI2.getY()+S2indexJ2.getX()+S2indexJ2.getY()+minDist2); double d1=Math.min(minDist1,minDist2); *在S1,S2中收集距离中线两侧小于dl的点,分别存在P1和P2中 ArrayList pp1=new ArrayList(); pp2=new ArrayList if(mid-S1i.getX() d1) pp1.add(S1i); if(S2i.getX(
11、)-mid) pp2.add(S2i); Point P1=new Pointpp1.size(); Point P2=new Pointpp2.size(); pp1.toArray(P1); pp2.toArray(P2); *将P1和P2中的点按Y坐标升序排列 sortY(P1); sortY(P2); *求解P1和P2两者之间可能的最近对距离 double d2=Double.POSITIVE_INFINITY; P1.length; for(int j=0; P2.length; if(Math.abs(P1i.getY()-P2j.getY() double temp=Math.s
12、qrt(Math.pow(P1i.getX()-P2j.getX(),2)+Math.pow(P1i.getX()-P2j.getX(),2); if(temppj+1.getX() int t=pj.getX(); pj.setX(pj+1.getX(); pj+1.setX(t); int n=pj.getY(); pj.setY(pj+1.getY(); pj+1.setY(n); *设计按点Point的y坐标升序排列的函数sortY public static void sortY(Point p) if(pj.getY()pj+1.getY() int t=pj.getY(); pj+1.setY(t); int n=pj.getX(); pj+1.setX(n);/* * 建立自己的类Pointclass Point implements Cloneable public Point() x=0; y=0; public Point(int x,int y) this.x=x; this.y=y; public void setX(int x) public void setY(int y) public int getX() return x; public int getY() return y; private int x; private int y;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1