#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
*
*/
struct Point
{
int x;
int y;
};
struct Point pt[10001];
int compare_x(const void* a,const void* b)
{
struct Point* A = (struct Point*) a;
struct Point* B = (struct Point*)b;
if (A->x == B->x)
return 0;
else if (A->x > B->x)
return 1;
else
return -1;
}
int compare_y(const void* a,const void* b)
{
struct Point* A = (struct Point*)a;
struct Point* B = (struct Point*)b;
if (A->y == B->y)
return 0;
else if (A->y > B->y)
return 1;
else
return -1;
}
float distance(struct Point a,struct Point b)
{
return sqrt (( (a.x-b.x)*(a.x - b.x) ) + ( (a.y-b.y)*(a.y-b.y) ));
}
float divide_conquer(struct Point pt[],int l,int r)
{
if (l==r)
{
return 0;
}
if ((r-l)==1)
{
return distance(pt[l],pt[r]);
}
else if ((r - l) == 2)
{
int i,j ; float min_dist; float tmp;
min_dist = distance(pt[l],pt[r]);
for (i = l ; i < r; i++)
for (j = i+1; j <= r; j++)
{
tmp = distance(pt[i],pt[j]);
if (tmp < min_dist)
min_dist = tmp;
}
return min_dist;
}
else
{
float u,v; int m; float min_dist; int i,j, k;
m = (l+r) / 2;
u = divide_conquer(pt,l,m);
v = divide_conquer(pt,m,r);
min_dist = (u < v ? u : v);
if (m-1 >= l)
{
for (i = m-1; i >= l; i--)
if (distance(pt[m],pt[i]) > min_dist) break;
}
if (m+1 <= r)
{
for (j = m+1; j <= r; j++)
if (distance(pt[m],pt[j]) > min_dist) break;
}
for ( ; i < j; i++)
for (k = i+1; k <= j; k++)
{
u = distance(pt[i],pt[k]);
if (u < min_dist)
min_dist = u;
}
return min_dist;
}
}
float brute_force(struct Point ar[],int N)
{
int i,j;
float min_dist,tmp;
min_dist = distance(ar[0],ar[1]);
for (i=0;i<N-1;i++)
for (j=i+1;j<N;j++)
{
tmp = distance(ar[i],ar[j]);
if (tmp < min_dist)
min_dist = tmp;
}
return min_dist;
}
int main()
{
int N; int i; float d;
freopen("input.in","r",stdin);
while (1)
{
fscanf(stdin,"%d",&N);
if (N == 0 ) break;
for (i = 0 ; i < N; i++)
{
fscanf(stdin,"%d %d",&pt[i].x,&pt[i].y);
}
qsort(pt,N,sizeof(struct Point),compare_x);
d = divide_conquer(pt,0,N-1);
if (d < 10000)
printf("%.4f\n",d);
else
printf("INFINITY/n");
You don't understand that piece of code -- don't waste your time figuring it out. You really are better off just doing your homework yourself, from scratch. You'll spend less time on it, you'll get more out of it, and you'll get a better grade (assuming you don't fail for cheating).
Frankly, that code isn't very good to begin with...