分治法求最近对未完成

#include <iostream>
using namespace std;
struct point
{
	int x,y;

};
void change(point a,point b)
{
	point temp;
	/*temp.x=a.x;
	temp.y=a.y;
	a.x=b.x;
	a.y=b.y;
	b.x=temp.x;
	b.y=temp.y;*/
	temp=a;
	a=b;
	b=temp;	
	cout<<"("<<a.x<<","<<a.y<<")"<<"-->"<<"("<<b.x<<","<<b.y<<")"<<endl;	
}

int Partition(int r[],int first, int end)
{
	int i=first,j=end;
	while(i<j)
	{
			while(i<j&&r[i]<=r[j])
			{
				j--;
			}
			if(i<j)
			{
				int temp=r[i];
				r[i]=r[j];
				r[j]=temp;
				i++;
				
				cout<<r[i]<<"->"<<r[j]<<endl;
	
			}
			while(i<j&&r[i]<=r[j])
			{
				i++;
			}
			if(i<j)
			{
				int temp=r[i];
				r[i]=r[j];
				r[j]=temp;
				j--;

			}
	}
	return i;
}
int PartitionStruct(point r[],int first, int end)
{
	int i=first;
	int j=end;
	while(i<j)
	{
			while(i<j&&r[i].y<=r[j].y)
			{
				j--;
			}
			if(i<j)
			{
				//change(r[i],r[j]);
				point temp=r[i];
				r[i]=r[j];
				r[j]=temp;
				//print(r,7);//
				i++;

			}
			while(i<j&&r[i].y<=r[j].y)
			{
				i++;
			}
			if(i<j)
			{
				//change(r[i],r[j]);
				point temp=r[i];
				r[i]=r[j];
				r[j]=temp;
				//print(r,7);//
				j--;

			}
	}
	return i;
}
void QuickSort(int r[],int first,int end)
{
	int pivot;
	if(first<end)
	{
		pivot=Partition(r,first,end);
		QuickSort(r,first,pivot-1);
		QuickSort(r,pivot+1,end);
	}

}

void QuickSortStruct(point r[],int first,int end)
{
	int pivot;
	if(first<end)
	{
		pivot=PartitionStruct(r,first,end);
		//cout<<"输出pivot"<<pivot<<endl;
		QuickSortStruct(r,first,pivot-1);
		QuickSortStruct(r,pivot+1,end);
	}
		
}
double closest(point s[],int low ,int high,point r[],point w[],double j[],int *rot,int *wot,int *jot)
{
	
	
	double d1,d2,d3,d;
	double p1,p2,p3;
	if(high-low==1)
	{
		r[*rot].x=s[low].x;//
		r[*rot].y=s[low].y;//
		(*rot)++;
		w[*wot].x=s[high].x;//	
		w[*wot].y=s[high].y;//
		(*wot)++;
		j[(*jot)++]=sqrt(pow((s[low].x-s[high].x)*1.0,2)+pow((s[low].y-s[high].y)*1.0,2));//
		cout<<"最近点对为++++"<<"("<<s[low].x<<","<<s[low].y<<")  "<<"("<<s[high].x<<","<<s[high].y<<") "<<endl;
		//return distance(s[low],s[high] );
		cout<<"----------"<<s[low].x<<" "<<s[low].y<<" "<<s[high].x<<" "<<s[high].y<<endl;
		return sqrt(pow((s[low].x-s[high].x)*1.0,2)+pow((s[low].y-s[high].y)*1.0,2));
	}
	
	if(high-low==2)
	{
		int t1=0,t2=0;
		//p1=distance(s[low],s[high]);
		//p2=distance(s[low+1],s[high]);
		//p3=distance(s[low],s[low+1]);
		cout<<s[low].x<<s[low].y<<" "<<s[low+1].x<<s[low+1].y<<" "<<s[high].x<<s[high].y<<endl;
		p1= sqrt(pow((s[low].x-s[high].x)*1.0,2)+pow((s[low].y-s[high].y)*1.0,2));
		p2= sqrt(pow((s[low+1].x-s[high].x)*1.0,2)+pow((s[low+1].y-s[high].y)*1.0,2));
		p3= sqrt(pow((s[low].x-s[low+1].x)*1.0,2)+pow((s[low].y-s[low+1].y)*1.0,2));
		double min=999.0;
		if(p1<min)
		{
			min=p1;
			t1=low;
			t2=high;
			cout<<"#"<<p1<<endl;
		}
		if(p2<min)
		{
			min=p2;
			t1=low+1;
			t2=high;
			cout<<"#"<<p2<<endl;
		}
		if(p3<min)
		{
			min=p3;
			t1=low;
			t2=low+1;
			cout<<"#"<<p3<<endl;
		}
		r[*rot].x=s[t1].x;//
		r[*rot].y=s[t1].y;//
		(*rot)++;
		w[*wot].x=s[t2].x;//
		w[*wot].y=s[t2].y;//
		(*wot)++;
		j[(*jot)++]=min;//
		d=min;
		cout<<"最近点对为---"<<"("<<s[t1].x<<","<<s[t1].y<<")  "<<"("<<s[t2].x<<","<<s[t2].y<<") "<<endl;
		return d;
	}
	int mid=(low+high)/2;
	d1=closest(s,low,mid,r,w,j,rot,wot,jot);
	d2=closest(s,mid+1,high,r,w,j,rot,wot,jot);
	d=min(d1,d2);
	cout<<"mid"<<d<<endl;
	int index=0;
	point *p=new point[7];
	for(int i=mid;(s[mid].x -s[i].x<d)&&(i>=low);i--)
	{
		p[index]=s[i];
		//cout<<s[i].x<<" "<<s[i].y<<"    ";
		index++;
	}
	//cout<<"---"<<endl;
	for(int i=mid+1;(s[i].x-s[mid].x<d)&&(i<=high);i++)
	{
		p[index]=s[i];
		//cout<<s[i].x<<" "<<s[i].y<<"    ";
		index++;
	}
	QuickSortStruct(p,0,index-1);
	/*cout<<endl;
	cout<<"*"<<endl;
	cout<<endl;
	for(int i=0;i<index;i++)
	{		
		cout<<p[i].x<<" "<<p[i].y<<"       ";		
	}
	cout<<endl;
	cout<<"*"<<endl;
	cout<<endl;*/
	int q1=0,q2=0;
	for(int i=0;i<index-1;i++)
	{
		for(int j=i+1;j<index;j++)
		{
			if(abs(p[i].y-p[j].y)>d)
				break;
			if( sqrt(pow((p[i].x-p[j].x)*1.0,2)+pow((p[i].y-p[j].y)*1.0,2))<d)
			{
				//d=distance(p[i],p[j]);
				d=sqrt(pow((p[i].x-p[j].x)*1.0,2)+pow((p[i].y-p[j].y)*1.0,2));
				q1=i;
				q2=j;

			}
		}
	}
	if(q1!=0||q2!=0)
	{
		r[*rot].x=s[q1].x;//
		r[*rot].y=s[q1].y;//
		(*rot)++;
		w[*wot].x=s[q2].x;//	
		w[*wot].y=s[q2].y;//	
		(*wot)++;
		j[(*jot)++]=sqrt(pow((p[q1].x-p[q1].x)*1.0,2)+pow((p[q1].y-p[q2].y)*1.0,2));//
		//cout<<"最近点对为"<<"("<<p[q1].x<<","<<p[q1].y<<")  "<<"("<<p[q2].x<<","<<p[q2].y<<") "<<endl;
	}
	
	return d;
	

}
double distance(point a,point b)
{
	return sqrt(pow((a.x-b.x)*1.0,2)+pow((a.y-b.y)*1.0,2));

}


int main()
{
	int n;
	cout<<"输入n"<<endl;
	cin>>n;

	point *s=new point[n];
	cout<<"依此输入数组初始元素x"<<endl;
	for(int i=0;i<n;i++)
	{
		cin>>s[i].x;
	}
	cout<<"依此输入数组初始元素y"<<endl;
	for(int i=0;i<n;i++)
	{
		cin>>s[i].y;
	}
	
	//QuickSortStruct(s,0,n-1);
	point *r=new point[n];//
	point *w=new point[n];
	double *j=new double[n];
	int rot=0;
	int wot=0;
	int jot=0;
	int *crot=&rot;
	int *cwot=&wot;
	int *cjot=&jot;
	cout<<"最近距离为"<< closest( s, 0 , n-1,r,w,j,crot,cwot,cjot)<<endl;
	double MIN=999.0;
	int f=0;
	//cout<<"jot "<<jot<<endl;
	for(int i=0;i<jot;i++)
	{
		cout<<"("<<r[i].x<<","<<r[i].y<<")  "<<"("<<w[i].x<<","<<w[i].y<<") "<<endl;
		//if(j[i]<MIN)
		//{
		//	f=i;
		//	MIN=j[i];
		//}
		//cout<<"**"<<endl;
	}
	cout<<"最近对为"<<"("<<r[f].x<<","<<r[f].y<<")  "<<"("<<w[f].x<<","<<w[f].y<<") "<<endl;
	
	/*int *r=new int[n];
	cout<<"依此输入数组初始元素"<<endl;
	for(int i=0;i<n;i++)
	{
		cin>>r[i];
	}
	QuickSort(r,0,n-1);
	for(int i=0;i<n;i++)
	{
		cout<<r[i]<<" ";
	}*/

	system("pause");


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值