The beauty of Programming: Chapter II the charm of the number 2.11 looking for the nearest point to _ programming beauty

Source: Internet
Author: User
Tags array sort
/* Find the nearest point to: Analysis and solution: simple to difficult: first look at the situation: in a number of arrays, how to quickly find the number of n 22 difference in the minimum value? Solution 1: The total number of n in the array, we have their 22 between the difference to find out, we can draw the minimum value.

The time complexity of O (n^2) expands to two dimensions, which is equivalent to enumerating any two points and then recording the nearest point pair. Solution 2: If the array is ordered, it is easy to find the smallest difference. You can use the O (NLOGN) algorithm to sort, after sorting, find the minimum difference only need O (n) time but can not be extended to two-dimensional, because the nearest point is not guaranteed to be mapped to a line immediately after the two points. For example, the distance between point A and point C is recent, but their projection points on the x-axis are not adjacent to the solution 3: Divide the array into left,right two parts with the median k of the array, less than the left part of K, and the other is the right part, then the most deserted value comes from the left part, Either from the right part or the largest number in the left and the difference between the smallest number in the left. Using the idea of divide and conquer, the time complexity of O (NLOGN) According to the horizontal direction of the coordinates of the plane of the N points into two parts, hope that the number of these two-part points is similar. Suppose that the shortest distance between the nearest point pairs in the left and right two parts is mindist and Mindist (right), and one is not considered, a point comes from the left part, and the other point comes from the well part.
Consider only the candidate pairs that are likely to be the nearest point pair.

If the point between left and right is more than mdist = min (left), and Mindist is right), then they must not be the nearest point pair. By dividing all the points into x<m and x>m through a straight line x = m, we need to consider a pair of CDs after finding the nearest pair of two parts respectively. That is, you only need to consider the minimum point pairs between x = m-mdist to x = M + mdist, and then compare it to mdist. When calculating the minimum point pairs in a banded region, you can sort the vertices in the Ribbon region according to Y coordinates, and if a point pair is less than mdist, then they must be in a mdist* (2*mdist) region, and within the left and right two mdist*mdist square regions, There are only 4 points at most. If there are more than 4 points, the distance of at least one point pair in this square area is less than mdist, which is the nearest point to the X < m and x > M two sections is Mindist (left) and Mindist (righT) contradictory.
Therefore a mdist* (2*mdist) region has a maximum of 8 points, and for any vertex within a banded region, it is OK to look at the distance between it and the 7 points that are sorted by Y coordinates and next to each other.

You can use the merge sort method to sort the points of the banded region by Y coordinate, and the merging sort process is combined with the algorithm of calculating the nearest point pair. Input://6//5 6 8 3 7 9//output://1 input: 5-2 2-1 1 1 0 2 2 3 5 output: 1.4/#include <stdio.h> #include <iostream&
Gt
#include <algorithm> #include <math.h> using namespace std;
const int MAXSIZE = 10000;

const int INF = 0x7fffffff;
	typedef struct Point {double _dx,_dy;
	int _iindex;
		Point (): _dx (0.0), _dy (0.0), _iindex (0) {} void set (double x,double y) {_dx = x;
	_dy = y;
}}point;
Point Xpointarr[maxsize];

Point Ypointarr[maxsize];

Double min (double a,double b) {return a < b a:b;}

BOOL Cmpx (const point& point1,const point& point2) {return point1._dx < POINT2._DX;}

BOOL Cmpy (const point& point1,const point& point2) {return point1._dy < Point2._dy;} Double distance (const point& Point1,const point& point2) {return sqrt (POINT1._DX-POINT2._DX) * (point1._dx-p OINT2._DX) + (point1._dy-point2._dy) * (Point1._dy-point2._dy));
	Double mindistance (int low,int high,point* ppoint)//does not understand {double dret = INF; if (high-low <= 2)//When high-low = 2, for example, high = 3,low = 1, then calculate the distance of all the points inside, that is, only 3 nodes or 2 nodes, directly calculate who is the minimum value {for (int i = Lo W; I < high;
				i++) {for (int j = i + 1; j <= High; j + +) {Double DDIs = distance (xpointarr[i],xpointarr[j]);
				if (DDIs < Dret) {Dret = DDIs;
	}} return Dret; 
	int mid = (low + high)/2;//Division point* YL = new Point[mid-low + 1];//This is an equal-point group point* YR = new point[high-mid];//?
	int i,j,k,ind; for (i = 0, j = 0,k = 0; I <= high-low; i++)//equalization point group {if (Ppoint[i]._iindex <= mid)//mid the middle subscript, starting with the points that will be sorted according to Y coordinates, and half row
		Divide to the left, half to the right {yl[j++] = Ppoint[i];
		else {yr[k++] = Ppoint[i];
	Double left_min = mindistance (low,mid,yl);//recursive solution minimum point pair distance double right_min = mindistance (mid+1,high,yr);
	Dret = min (left_min,right_min); Double dline = xpointarr[mid]._dx;//uses the horizontal coordinate of the original intermediate node for (i = 0, ind = 0; I <= high-low; i++) {if (Fabs (Ppoint[i]._dx-dline) < Dret)//Compare the distance value of a node array distance from the middle line {ppoint[ind++] = ppoint[i];//? nodes that collect distances less than d from the Central line , save to Ypointarr} for (i = 0; i < ind-1 i++)//Compare the distance between a vertex and the remaining 7 vertices {for (j = i + 1; J <= I+7 && J &L T Ind
			J + +) {Double dtemp = distance (ppoint[i],ppoint[j]);
			if (Dtemp < Dret) {Dret = dtemp;
	}} delete[] YL;
	Delete[] YR;
return dret; }////float mindifference (float* parr,int ilen)//{/if (!parr | | Ilen <= 0)//{//return inf*1.0;//}//Float D
Ret = ABS (Parr[0]-parr[1]); for (int i = 2; i < Ilen i++)//{/if (ABS (PARR[I-1)-parr[i)) < Dret)//{//Dret = ABS (Parr[i-1]-PA
Rr[i]);
}//}//Return dret; //void process ()//{//int n;/while (EOF!= scanf ("%d", &n))//{//float iarr[maxsize];//for (int i = 0; I < n;
i++)//{//scanf ("%f", &iarr[i]);//}/Sort (iarr,iarr+n); printf ("%.2f\n", Mindifference (Iarr, n));
	}//} void Process2 () {int n;
		while (EOF!= scanf ("%d", &n)) {memset (xpointarr,null,sizeof (Xpointarr));
		memset (ypointarr,null,sizeof (Ypointarr));
		for (int i = 0; i < n; i++) {scanf ("%lf%lf", &xpointarr[i]._dx,&xpointarr[i]._dy);
		Sort (xpointarr,xpointarr+n,cmpx); for (int i = 0; i < n; i++) {Xpointarr[i]._iindex = i;//set subscript array sort order} memcpy (ypointarr,xpointarr,sizeof NT)///Copy the array sort (ypointarr,ypointarr+n,cmpy) sorted by the horizontal axis to the ordinate sort (*n);/= use two sorting principles and sort//printf in Y coordinates ("%.2lf\n",
	Mindistance (0,n-1,ypointarr))//Since the transmission is n-1, which means that n-1 is actually used} int main (int argc,char* argv[]) {process2 ();
	GetChar ();
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.