Article From:https://www.cnblogs.com/zuoyou151/p/9059903.html

Let p1= (x1, Y1), p2= (X2, Y2)… Pn= (xn, yn) be the set of YN that is formed by the n points on plane n. Recently, we ask you to find out the closest pair of points in the set.

Branch strategy:

（1）Partition: the set S is divided into two subsets, S1 and S2. According to the principle of the balanced sub problem, each subset has about n/2 points, and the nearest point pair of set S is pi and PJ (1< =i, j< =n).

There are three kinds of cases: 1.pi S1, PJ S1

2.pi∈S1,pj∈S2

3.pi∈S2, pj∈S2

（2）Solving sub problems: 1 and 2 recursive solutions for the classification stage.

Through the straight line x=M (median), the space is divided into two parts, x&lt, M and x> M respectively for the nearest left to right distance, D1, D2, and d=min (D1, D2).

Then we only need to consider 3, that is, the closest pair of points between X-D and x+d, and sort the points in Y according to the coordinates, if a point pair is the distance.

Less than D, they must be in the area of d* (2*d). For the algorithm nearest_pair (S, left, right)

1.Preprocessing: set point S={(x1, Y1), (X2, Y2)… (xn, yn)} in ascending order according to x coordinates.

2.If n=2, then return the distance between the points

3.m

Calculate the nearest distance between {(x1, Y1), (X2, Y2)…… (XM, YM)}.

Calculate the closest distance between {(XM, YM) (xm+1, ym+1)… (xn, yn)}.

d=min(d1,d2)

4.Calculate the nearest point pair of S[l… R] in sequence (set the Y in ascending order, examine the point of y-s[m].y&lt, d).

l=min(i)| S[m]-S[i]<d

r=max(i)| S[i]-S[m]<d

`// Experiment four.Cpp: defines the entry point of console application./ / the latest problem//2018.4.18#include "stdafx.h"#include< iostream>#include≪ stdio.h>#include< stdio.h>#include< algorithm>#include< math.h>#include<Windows.h>Using namespace STD;Struct point {Double x;Double y;}P;Double distance (POInt P1, point P2) {Return sqrt ((p1.x - p2.x) * (p1.x p2.x) + (p1.y p2.y) * (p1.y p2.y));}BoolCMP1 (point P1, point P2) {Return p1.x < p2.x;}Bool CMP2 (point P1, point P2) {Return p1.y &lT; p2.y;}/ brute force methodDouble get_min (int n){Double min = sqrt ((P.x - P.x) * (P.x P.x) + (P).y - P.y) * (P.y - P.y))For (int i = 0; I < n; i++) {For (int j = I + 1; J < nJ++) {Double T = sqrt ((P[i].x - P[j].x) * (P[i].x P[j].x) + (P[i].y P[j].y) * (P[i].y P[j].y)))If (min> t)Min = t;}}Return min;}/ / division methodDouble nearest_pair (point S[], int LEF)T, int right) {Cout < < left < < "<"; < right < < <If (right-left = = 1){Return distance (S[right], S[left]);}If (right - left = = 2) {Double D1 = distance (S[right]S[left]);Double D2 = distance (S[right], S[right + 1]);Double D3 = distance (S[right + 1], S[l)Eft]);D2 = min (D1, D2);D3 = min (D2, D3);Return D3;}Int m = (right+left) / 2;Double D1= nearest_pair (S, left, m);Double D2 = nearest_pair (S, m+1, right);//sort (S+right, S+left, CMP2);Double D = min (D1, D2);Int l = left, r = right;While (S[l].x < S[m].x - D & & l <= right);L++;While (S[r].x > S[m].x + D & & r> =left)R++;Sort (S + 1, S + R + 1)CMP2);Double D3;For (int i = L; I < = R; i++) {For (int j = I + 1; J < = R; j++) {IF (S[j].y - S[i].y > = D) {Break;}Else {D3 = distance (S[i], S[j]);If (D3 &l)T; d)D = D3;}}}Return D;}Int main (){Int n;Cout < < "Input n:";CIN >> n;For (int i = 1; I < = n; i++) {Cout < < "Input the" < < I < < th thBer: ";CIN > > P[i].x > > P[i].y;}Sort (P + 1, P + n+1, CMP1);For (int i = 1; I <); = n; i++) {Cout < < P[i].x < < "<"; < P[i].y < < <}Double M = get_mIn (n);Cout < < m < < endl;Double M2 = nearest_pair (P, 1, n);Cout < < M2 < <Endl;System ("pause");Return 0;}`