Home > Back-end >  Don't know what problems, with partition method on recently
Don't know what problems, with partition method on recently

Time:09-30

#include
#include
#include
#include
#include
using namespace std;
Typedef struct {
Float x;//point of x, y coordinates
Float y;
} POINT;
//the structure of the auxiliary point
Typedef struct {
Int index;//points in the x array subscript
Float x;//point of x, y coordinates
Float y;
} A_POINT;
Bool compareX (POINT a to POINT b)
{
Return b.x & gt; A.x;
}
Bool compareY (A_POINT a, A_POINT b)//to sorting the comparison of the auxiliary point
{
Return b.y & gt; A.y;
}
Float dist (POINT a to POINT b)//calculating the distance between the two square
{
Float dx, dy;
Dx=a.x - b.x;
Dy=a.y - b.y;
Return (dy dx + dy dx * *);
}
Void closest (POINT X [], A_POINT Y [], int low, int, POINT & amp; A, POINT & amp; B, float & amp; D)
{
Int I, j, k, m;
POINT of al, bl, ar, br;
Float dl, Dr, dx;
If ((high - low)==1)
{
A=X [low];
B=X [high];
D=dist (X [low], X [high]);
}
The else
{
If ((high - low)==2)
{
Dl=dist (X [low], X + 1] [low);
Dr=dist (X [low], X [low] + 2);
Dx=dist (X + 1] [low, X [low] + 2);
If ((dl<=(Dr) & amp; & (dl<=d))
{
A=X [low];
B=X + 1] [low;
D=dl;
}

The else
{
If (dr<=d)
{
A=X [low];
B=X [low + 2];
D=Dr;
}
The else
{
A=X + 1] [low;
B=X [low + 2];
D=dx.
}
}
}
The else//when n> 3 when to partition
{
A_POINT * SL=new A_POINT [(high - low)/2 + 1];
A_POINT * SR=new A_POINT (high - low)/2;
M=(high - low)/2;//the x array by a m is divided into two and a half
J=k=0;
for(i=0; I<=high - low; I++)
{
If (Y [I] index<=m)
{
SL [j++]=Y [I];//collect the left subset of recent point to

}
The else
{
The SR=Y [k++] [I];//to collect the right subset of closest point
}
}
The closest (X, SL, low, m, al, bl, dl);//calculate the left a subset of the closest point to
Closest (X, SR, m + 1, high, ar, br, Dr);//calculate the right to a subset of the closest point
If (dl{
A=al; B=bl; D=dl;
}
The else
{
A=ar; B=br; D=Dr;
}
POINT * Z=new POINT (high - low, + 1];
K=0;
for(i=0; I<=high - low; I++)//collect distance line distance less than d elements, save to the array Z
{
If (fabs (X [m]. X-y [I]. X) & lt; D)
{
Z [k]. X=Y [I]. X.
Z [k++] y=y [I] y;
}

}
for(i=0; I{
for(j=i+1; (j[I] dl=dist (Z, Z) [j].
If (dl{
A=Z [I];
B=Z [j];
D=dl;

}


}
}
//delete SL;
//delete the SR;
//delete Z;
}

}
Void closest_pair (POINT X [], int n, POINT & amp; A, POINT & amp; B, float & amp; D)
{
If (n<2)
{
D=0;
}
The else
{
Sort (X, X + n, compareX);//to sorting the x array
A_POINT * Y=new A_POINT [n].//initialize the auxiliary points set Y

For (int I=0; I & lt; N; I + +)
{
[I] Y index=I;
[I] Y=x x [I]. X.
[I] Y Y=X [I] Y;
}
Sort (Y, Y + n, compareY);//to sorting y array
Closest (X, Y, 0, n - 1, a, b, d);//please close point to
D=SQRT (d);//will be d kaiping before the real distance between two points is
The delete Y;
}
}
Int filearrayload (POINT * X)
{
Int num1.
Ifstream infile ". TXT ", the ios: : (in);//file into the program folder (not a must)
if (! Infile)
{
Cout & lt; <"The open file error!" exit(1);
}
int i=0;
For (I=0; ! Infile. Eof (); I++)
{
Infile & gt;> [I]. X X & gt;> X [I] y;
}
Num1=I;
Infile. Close ();
Return num1.
}
Int main ()
{
int n;
POINT * X=new POINT [n].
//n=filearrayload (X);
POINT a, b;
Float d;
Closest_pair (X, n, a, b, d);
If (n>
=2){
Cout<& lt;" (" & lt; Cout<& lt;" Recently some distance is: "& lt;
}
The else
{
Cout<& lt;" There is no point to recently!" }
Delete the X;
return 0;

}
  • Related