#include
#include
#include
#include
#include
#include
#include
#define LEFT_BOUND 0.0
#define RIGHT_BOUND 100.0
#define LOWER_BOUND 0.0
#define UPPER_BOUND 100.0
using namespace std;
struct Spot
{
float x;
float y;
Spot(float x, float y) : x(x), y(y) {};
};
typedef vector Spots;
inline bool xAxisCompare(const Spot& a, const Spot& b)
{
return a.x < b.x;
}
inline float calDist(const Spot& a, const Spot& b)
{
return sqrtf(powf(a.x - b.x, 2.0) + powf(a.y - b.y, 2.0));
}
float bruteForce(const Spots& sortedSpots, size_t left1, size_t right1, size_t left2, size_t right2)
{
float minDist = numeric_limits::max();
for (size_t i = left1; i <= right1; i++)
{
for (size_t j = left2; j <= right2; j++)
{
const float dist = calDist(sortedSpots[i], sortedSpots[j]);
if (dist < minDist)
{
minDist = dist;
}
}
}
return minDist;
}
float getMinDist(const Spots& sortedSpots, size_t left, size_t right)
{
if (left == right)
{
return numeric_limits::max();
}
if (left + 1 == right)
{
return calDist(sortedSpots[left], sortedSpots[right]);
}
const size_t leftRight = (left + right) / 2, rightLeft = leftRight + 1;
const float leftMin = getMinDist(sortedSpots, left, leftRight), rightMin = getMinDist(sortedSpots, rightLeft, right);
const float smallerOne = min(leftMin, rightMin);
if (sortedSpots[rightLeft].x - sortedSpots[leftRight].x >= smallerOne)
{
return smallerOne;
}
const float midBound = (sortedSpots[leftRight].x + sortedSpots[rightLeft].x) / 2.0;
const float leftBound = midBound - smallerOne, rightBound = midBound + smallerOne;
size_t i = leftRight, j = rightLeft;
while (i > left && sortedSpots[i - 1].x > leftBound)
{
i--;
}
while (j < right && sortedSpots[j + 1].x < rightBound)
{
j++;
}
return min(smallerOne, bruteForce(sortedSpots, i, leftRight, rightLeft, j));
}
float getMinDist(const Spots& spots)
{
const size_t num = spots.size();
if (num == 0)
{
return numeric_limits::max();
}
auto sortedSpots = spots;
sort(sortedSpots.begin(), sortedSpots.end(), xAxisCompare);
const size_t left = 0, right = num - 1;
return getMinDist(sortedSpots, left, right);
}
int main(int argc, char* argv[])
{
Spots spots;
srand(time(nullptr));
cout << "30 spots" << endl;
for (size_t i = 0; i < 30; i++)
{
const float x = LEFT_BOUND + (float)rand() / (float)RAND_MAX * (RIGHT_BOUND - LEFT_BOUND);
const float y = LOWER_BOUND + (float)rand() / (float)(RAND_MAX) * (UPPER_BOUND - LOWER_BOUND);
spots.emplace_back(x, y);
cout << '(' << x << ", " << y << ')' << endl;
}
cout << getMinDist(spots) << endl;
return 0;
}
问:const float x = LEFT_BOUND + (float)rand() / (float)RAND_MAX * (RIGHT_BOUND - LEFT_BOUND);这个函数什么意思
问:float getMinDist(const Spots& sortedSpots, size_t left, size_t right)
{
if (left == right)
{
return numeric_limits::max();
}
if (left + 1 == right)
{
return calDist(sortedSpots[left], sortedSpots[right]);
}
const size_t leftRight = (left + right) / 2, rightLeft = leftRight + 1;
const float leftMin = getMinDist(sortedSpots, left, leftRight), rightMin = getMinDist(sortedSpots, rightLeft, right);
const float smallerOne = min(leftMin, rightMin);
if (sortedSpots[rightLeft].x - sortedSpots[leftRight].x >= smallerOne)
{
return smallerOne;
}
const float midBound = (sortedSpots[leftRight].x + sortedSpots[rightLeft].x) / 2.0;
const float leftBound = midBound - smallerOne, rightBound = midBound + smallerOne;
size_t i = leftRight, j = rightLeft;
while (i > left && sortedSpots[i - 1].x > leftBound)
{
i--;
}
while (j < right && sortedSpots[j + 1].x < rightBound)
{
j++;
}
return min(smallerOne, bruteForce(sortedSpots, i, leftRight, rightLeft, j));
}
float getMinDist(const Spots& spots)
{
const size_t num = spots.size();
if (num == 0)
{
return numeric_limits::max();
}
auto sortedSpots = spots;
sort(sortedSpots.begin(), sortedSpots.end(), xAxisCompare);
const size_t left = 0, right = num - 1;
return getMinDist(sortedSpots, left, right);
}这个函数是什么意思
尤其是这个if (left == right)
{
return numeric_limits::max();
}
if (left + 1 == right)
{
return calDist(sortedSpots[left], sortedSpots[right]);
}
希望能讲得详细点