gpt4 book ai didi

c++ - 在 C++ 中实现 "Closest pair of points"的问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:48:20 25 4
gpt4 key购买 nike

我正在尝试根据 Cormen 书和维基百科文章在 C++ 中实现最近的点对,我认为该算法是正确的,但它只适用于非常小的数据。代码如下:

#include <cstdio>
#include <algorithm>
#include <cmath>
#define REP(i,n) for(int i=0;i<n;i++)

using namespace std;

struct point
{
long long x, y;
};
struct dist
{
long long x_1,y_1,x_2,y_2, distance;
} dis;

inline bool OrdX(const point &a, const point &b)
{
if(a.x==b.x)
{
return a.y<b.y;
}
return a.x<b.x;
}

inline int OrdY(const point &a, const point &b)
{
if(a.y==b.y)
{
return a.x<b.x;
}
return a.y<b.y;
}


// is - function that check is a an element of X_L array
inline bool is(const point &a, point *X_L, int p, int k)
{
if(p<=k)
{
int center = (p+k)/2;

if(X_L[center].x == a.x)
{
return true;
}
if(X_L[center].x > a.x)
{
return is(a, X_L, p, center-1);
}
else
{
return is(a, X_L, center+1, k);
}
}

return false;
}


// odl - function takes two points and return distance between them ^2
inline long long odl(const point &a, const point &b)
{
return ((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y));
}


int tmp;

// fun - function that returns the pair of closest points using divide & conquer
struct dist fun(int n, point *X, point *Y)
{
// if there are less that 4 points - it checks it using bruteforce
if(n<4)
{
if(odl(X[0], X[1]) < dis.distance)
{
dis.distance = odl(X[0],X[1]);
dis.x_1 = X[0].x;
dis.y_1 = X[0].y;
dis.x_2 = X[1].x;
dis.y_2 = X[1].y;
}

if(n==3)
{
if(odl(X[0], X[2]) < dis.distance)
{
dis.distance = odl(X[0],X[2]);
dis.x_1 = X[0].x;
dis.y_1 = X[0].y;
dis.x_2 = X[2].x;
dis.y_2 = X[2].y;
}
if(odl(X[1], X[2]) < dis.distance)
{
dis.distance = odl(X[1],X[2]);
dis.x_1 = X[1].x;
dis.y_1 = X[1].y;
dis.x_2 = X[2].x;
dis.y_2 = X[2].y;
}
}
}
// otherwise it divides points into two arrays and runs fun
// recursively foreach part
else
{
int p=n/2;

int PPP = (X[p].x + X[p-1].x)/2;


point *X_L = new point[p];
point *X_R = new point[n-p];
point *Y_L = new point[p];
point *Y_R = new point[n-p];

REP(i,p)
X_L[i] = X[i];

for(int r=p; r<n; r++)
{
X_R[r-p] = X[r];
}

int length_Y_L = 0;
int length_Y_R = 0;

REP(i,n)
{
if(is(Y[i], X_L, 0, p))
{
Y_L[length_Y_L++] = Y[i];
}
else
{
Y_R[length_Y_R++] = Y[i];
}
}


dist D_L = fun(p, X_L, Y_L);
dist D_R = fun(n-p, X_R, Y_R);
dist D;

if(D_L.distance < D_R.distance)
{
D = D_L;
}
else
{
D = D_R;
}

tmp = 0;
point *Y2 = new point[n];

double from = sqrt((double)D.distance);

for(int r=0; r<n; r++)
{
if(Y[r].x > (long long)PPP-from && Y[r].x < (long long)PPP + from)
{
Y2[tmp++] = Y[r];
}
}

//--tmp;
//int xxx = min(7, tmp-r);
int r = 0;
for(int j=1; j<min(7, tmp-r); j++)
{
if(odl(Y2[r], Y2[r+j]) < D.distance)
{
D.distance = odl(Y2[r], Y2[r+j]);
D.x_1 = Y2[r].x;
D.y_1 = Y2[r].y;
D.x_2 = Y2[r+j].x;
D.y_2 = Y2[r+j].y;
}
r++;
}

dis = D;

}
return dis;
}

int main()
{
int n;

n = 7;

point *X = new point[n];
point *Y = new point[n];

for(int i=0; i< 7; i++)
{
X[i].x = 0;
X[i].y = 10*i;
}

/*
REP(i,n)
{
scanf("%lld %lld", &X[i].x, &X[i].y);
}
*/
sort(X, X+n, OrdX);

REP(i,n)
Y[i] = X[i];

sort(Y, Y+n, OrdY);

dis.distance = odl(X[0], X[1]);

dis.x_1 = X[0].x;
dis.y_1 = X[0].y;
dis.x_2 = X[1].x;
dis.y_2 = X[1].y;

dist wynik = fun(n, X, Y);

printf(" %lld %lld\n %lld %lld\n", wynik.x_1, wynik.y_1, wynik.x_2, wynik.y_2);
return 0;
}

我得到这个错误:

 malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char
*) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct
malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size)
>= (unsigned long)((((__builtin_offsetof (struct malloc_chunk,
fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t)))
- 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end &
pagemask) == 0)' failed.

我已经尝试寻找对这个错误的解释,但找不到任何对我来说清楚的东西:/。你能帮我解决这个问题吗?谢谢

最佳答案

该消息表示您对动态分配的内存做了一些不好的事情。也许您释放了一个对象两次,或者写入的内存超出了类似数组的动态分配对象的开头或结尾。

在 Linux 上,工具 valgrind可能有助于查明程序执行的第一个地方,它发出了一个嘘声。

顺便说一句,你的宏:

#define REP(i,n) for(int i=0;i<n;i++)

定义不明确。 n 的替代应该加括号,因为 n可能是相对于 < 具有错误优先级的表达式运算符(operator)。例如:REP(i, k < m ? z : w) .你想要:

#define REP(var,n) for(int var=0;var<(n);var++)

var提醒程序员这个参数是一个变量名,而不是一个任意的表达式。

关于c++ - 在 C++ 中实现 "Closest pair of points"的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9967872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com