最近对问题

本文介绍了一种高效算法,用于在一个包含n个点的集合中找到最短距离的两个点,通过分治、划分和搜索策略,逐步缩小范围并优化查找过程。具体步骤包括按坐标排序、划分区间、子集内搜索等,最终返回最接近的点对距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 问题
    最近对问题要求在包含有n个点的集合S中,找出距离最近的两个点。设 p1(x1,y1),p2(x2,y2),……,pn(xn,yn)是平面的n个点。
    2解析
    1.按x坐标排列的n(n>=2)个点的集合S={(x1,y1),(x2,y2),…,(xn,yn)},
    2.如果n2,则返回(x1,y1)和(x2,y2)之间的距离,算法结束;
    3.如果n
    3,则返回(x1,y1)、(x2,y2)和(x3,y3)之间的最小距离,算法结束;
    4.划分:m==S中各点x坐标的中位数;
    5.d1 = 计算{(x1,y1),…,(xm,ym)}的最近对距离;d2 = 计算{(xm,ym),…,(xn,yn)}的最近对距离;
    6.d = min(d1,d2);
    7.依次考察集合S中的点p(x,y),如果(x<=xm 并且x>=xm-d),则将点p放入集合P1中;如果(x>xm 并且x<=xm+d),则将点p放入集合P2中;
    8.将集合P1和P2按y坐标升序排列;
    9.对集合P1和P2中的每个点p(x,y),在y坐标区间[y,y+d]内最对取出6个候选点,计算与点p的最近距离d3;
    10.返回min{d,d3};
    3设计
double closeset(int low, int high)
{
	if (low == high)   
		return MAX;
	if (low + 1 == high)
		return dist(p[low], p[high]);
	int mid = (low + high)>>1; 
	double ans = min(closeset(low, mid), closeset(mid+1, high)); 
	int i, j, c = 0;
	for (i = low; i <= high; i++) 
	{
		if (p[mid].x - ans <= p[i].x && p[i].x <= p[mid].x + ans)
			a[c++] = i;
	}
	sort(a, a + c, cmpy);
	for(i = 0; i < c; i++)
	{
		int k = i+7 > c ? c : i+7;  
		for (j = i+1; j < k; j++)
		{
			if (p[a[j]].y - p[a[i]].y > ans)  
				break;
			ans = min(dist(p[a[i]], p[a[j]]), ans);  
		}
	}
	return ans;
}

5源码

#include<iostream>
#include<math.h> 
#include<fstream> 
#include<algorithm>  
 
using namespace std;
#define MAX 0x3f3f3f3f
#define M 99999
 
struct point {
  double x, y;
}p[M];
 
int a[M];// 保存排序的索引
 
int cmpx(const point& a, const point& b)  //排序升序
{  
  return a.x < b.x;
}
 
int cmpy(int &a, int &b)   //排序升序
{
  return p[a].y < p[b].y;
}
 
inline double min(double a, double b)   //返回两个值中较小的
{
	return a < b ? a : b;
}
inline double dist(const point& a, const point& b)
{
  return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
double closeset(int low, int high)
{
	if (low == high)   
		return MAX;
	if (low + 1 == high)
		return dist(p[low], p[high]);
	int mid = (low + high)>>1; 
	double ans = min(closeset(low, mid), closeset(mid+1, high)); 
	int i, j, c = 0;
	for (i = low; i <= high; i++) 
	{
		if (p[mid].x - ans <= p[i].x && p[i].x <= p[mid].x + ans)
			a[c++] = i;
	}
	sort(a, a + c, cmpy);
	for(i = 0; i < c; i++)
	{
		int k = i+7 > c ? c : i+7;  
		for (j = i+1; j < k; j++)
		{
			if (p[a[j]].y - p[a[i]].y > ans)  
				break;
			ans = min(dist(p[a[i]], p[a[j]]), ans);  
		}
	}
	return ans;
}
 
int main()
{
	double totaltime;
	int n;
	double dmin;
	ifstream read_in;
	read_in.open("input.txt");
	read_in >> n;
	for(int i=0;i<n;i++)
	{
		cout<<"p"<<i+1<<":";
		read_in>>p[i].x>>p[i].y;
		cout<<p[i].x<<" "<<p[i].y<<endl;
	}
	sort(p,p+n,cmpx); //按照x轴排序
	dmin=closeset(0, n-1);
	cout<<"最近的距离是:"<<dmin<<endl;
	return 0;
}

/*
input
15
5 10
9 1
10 12
10 19
13 15
15 19
16 20
19 9
24 0
24 12
31 32
32 24
38 29
40 56
45 23
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值