• 大小: 994KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-20
  • 语言: C/C++
  • 标签: 最短距离  

资源简介

给定平面上的至少n个点(n〉=20),找出其中的一对点,使得在n个点组成的所有点对中,该点对间的距离最小。

资源截图

代码片段和文件信息

//平面上n个点之间的最短距离算法
/******************************
*         刘昂
*        2012年3月20日
******************************/


#include
#include 
#include 
#include 
#define MAX 100
#define MAXDISTANCE 32767
typedef struct{
int x;
int y;
}point;
point source[MAX]sortbyX[MAX]sortbyY[MAX]T[MAX];
void sortbyx()
{
int ijtempxtempy;
for (i=0;i {
for (j=0;j {
if(sortbyX[j].x>sortbyX[j+1].x)
{
tempx=sortbyX[j].x;tempy=sortbyX[j].y;
sortbyX[j].x=sortbyX[j+1].x;sortbyX[j].y=sortbyX[j+1].y;
sortbyX[j+1].x=tempx;sortbyX[j+1].y=tempy;
}
}
}
}
void sortbyy()
{
int ijtempxtempy;
for (i=0;i {
for (j=0;j {
if(sortbyY[j].y>sortbyY[j+1].y)
{
tempx=sortbyY[j].x;tempy=sortbyY[j].y;
sortbyY[j].x=sortbyY[j+1].x;sortbyY[j].y=sortbyY[j+1].y;
sortbyY[j+1].x=tempx;sortbyY[j+1].y=tempy;
}
}
}
}
double distance(point p1point p2)
{
return sqrt(pow((double)(p1.x-p2.x)2)+pow((double)(p1.y-p2.y)2));
}
double countdistance(int first int last ) 
{
double d0d1d2d3;
int midx0;
int ijk=0;
if (last-first+1<=3)
{
if (last-first+1==3)
{
d0=distance(sortbyX[first]sortbyX[first+1]);
d1=distance(sortbyX[first]sortbyX[last]);
d2=distance(sortbyX[first+1]sortbyX[last]);
if (d0<=d2&&d0<=d1)
{
return d0;
}
else if (d1<=d0&&d1<=d2)
{
return d1;
}
else
return d2;
}
else if (last-first+1==2)
{
return distance(sortbyX[first]sortbyX[last]);
}
else
return MAXDISTANCE;
}
else
{
mid=(first+last)/2;
x0=sortbyX[mid].x;
d1=countdistance(firstmid);
d2=countdistance(mid+1last);
d0=(d1>d2)? d2:d1;
d3=2*d0;
for (i=0;i {
if ((sortbyY[i].x-x0)<=0&&(sortbyY[i].x-x0)>=-d0||(sortbyY[i].x-x0)>0&&(sortbyY[i].x-x0)<=d0)
{
T[k]=sortbyY[i];
k++;
}
}
for (i=0;i {
for (j=i+1;j<=((i+7 {
d3=(distance(T[i]T[j]) }
}
d0=(d3 return d0;
}
}
void main()
{
int i;
double d;

srand((unsigned)time(NULL));
for (i=0;i {
source[i].x=rand()%MAX;
source[i].y=rand()%MAX;
}
for (i=0;i {
printf(“(%d%d)“source[i].xsource[i].y);
if(i%9==0)
printf(“\n“);
}
for (i=0;i {
sortbyX[i].x=sortbyY[i].x=source[i].x;
sortbyX[i].y=sortbyY[i].y=source[i].y;
}
sortbyx();
sortbyy();
printf(“\n“);
/*for (i=0;i<100;i++)
{
printf(“(%d%d)“sortbyY[i].xsortbyY[i].y);
if(i%9==0)
printf(“\n“);
}*/
d=countdistance(0MAX-1);
printf(“%f“d);
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      31744  2012-03-23 14:27  DistanceOfNPoints\Debug\DistanceOfNPoints.exe

     文件     356340  2012-03-23 14:27  DistanceOfNPoints\Debug\DistanceOfNPoints.ilk

     文件     437248  2012-03-23 14:27  DistanceOfNPoints\Debug\DistanceOfNPoints.pdb

     文件       1454  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\cl.command.1.tlog

     文件       4458  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\CL.read.1.tlog

     文件        882  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\CL.write.1.tlog

     文件        406  2012-03-20 09:54  DistanceOfNPoints\DistanceOfNPoints\Debug\DistanceOfNPoints.exe.embed.manifest

     文件        472  2012-03-20 09:54  DistanceOfNPoints\DistanceOfNPoints\Debug\DistanceOfNPoints.exe.embed.manifest.res

     文件        381  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\DistanceOfNPoints.exe.intermediate.manifest

     文件         89  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\DistanceOfNPoints.lastbuildstate

     文件       2508  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\DistanceOfNPoints.log

     文件      12456  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\DistanceOfNPoints.obj

     文件        224  2012-03-20 09:54  DistanceOfNPoints\DistanceOfNPoints\Debug\DistanceOfNPoints_manifest.rc

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link-cvtres.read.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link-cvtres.write.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.1568-cvtres.read.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.1568-cvtres.write.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.1568.read.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.1568.write.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.3900-cvtres.read.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.3900-cvtres.write.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.3900.read.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.3900.write.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.4692-cvtres.read.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.4692-cvtres.write.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.4692.read.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.4692.write.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.6480-cvtres.read.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.6480-cvtres.write.1.tlog

     文件          2  2012-03-23 14:27  DistanceOfNPoints\DistanceOfNPoints\Debug\link.6480.read.1.tlog

............此处省略34个文件信息

评论

共有 条评论

相关资源