Find the nearest point to __ nearest point to

Source: Internet
Author: User

Problem: Given the coordinates of n points on a plane, find the two nearest points.


Solution one. Brute force method (the difference between 22 to find out) time complexity O (n*n)


Solution Time Complexity O (N*LOGN) for solving 2:1-D cases


Solution III: The idea of divide and conquer, used in general situation

The thought is as follows: (1) dividing n points on the plane into two parts left and right according to the coordinate of horizontal direction

(2) Separating the middle region by Min (lfet,right) d

(3) Compare the y-coordinate of the middle region, and get the recent

Code references are as follows:

/** recent point to problem, time complexity O (N*LOGN)/#include <iostream> #include <cstdio> #include <cstring> #include <
Cmath> using namespace std;
Const double INF = 999999;

const int N = 100005;
    struct point {double x;
Double y;
}point[n];     
int n;
int tmpt[n];
    Compare the size of x,y coordinates under two-dimensional coordinates bool CMPXY (const point& A, const point& b) {if (a.x!= b.x) return a.x < b.x;
return A.Y < B.y; ///Compare the size of y-coordinate in two-dimensional coordinate system bool Cmpy (const int& A, const int& b) {return point[a].y < point[b].y;}//min value double

Min (double A, double b) {return a < b a:b;} Find the distance between two points double dis (int i, int j) {return sqrt (point[i].x-point[j].x) * (point[i].x-point[j].x) + (p
OINT[I].Y-POINT[J].Y) * (POINT[I].Y-POINT[J].Y));
    //Most recent pair of main functions double closest_pair (int left, int right) {double d = INF;
    if (left==right) return D;
    if (left + 1 = right) return to dis (left, right);
    int mid = (left+right) >>1; Double D1 = CloSest_pair (Left,mid); 
    Divide and treat recursive double d2 = Closest_pair (mid+1,right);//Divide the rule recursively d = min (d1,d2);
    int i,j,k=0; Separates the range for the width of D to (i = left; I <= right; i++) {if (Fabs (point[mid].x-point[i].x) <= D) TMP
    t[k++] = i;
    Sort (tmpt,tmpt+k,cmpy); Linear scan for (i = 0; i < K; i++) {for (j = i+1; J < K && Point[tmpt[j]].y-point[tmpt[i]].y<d ;
            J + +) {double d3 = dis (tmpt[i],tmpt[j]);
        if (d > d3) d = D3;
} return D;
        int main () {while (true) {scanf ("%d", &n);//Enter node number if (n==0) break;
        for (int i = 0; i < n; i++) scanf ("%lf%lf", &point[i].x,&point[i].y);
        Sort (POINT,POINT+N,CMPXY);
    printf ("%.2lf\n", Closest_pair (0,n-1)/2);
return 0; }




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.