An algorithmic question to find the closest point among the points.

Asked 2 years ago, Updated 2 years ago, 44 views

The execution environment uses the GCC compiler through Bash. We're going to create a program to find the distance between two of the 16 points that are closest to each other.

#include <stdio.h>
#include <math.h>
#define NUM_OF_DOTS 16
#define MIN(x,y) ((x < y) ? x : y)
// a point structure
typedef struct {
    int x;
    int y;
}DOT;

// Finding a Street
double distance (DOT a, DOT b) {
    double delta_x = a.x - b.x;
    double delta_y = a.y - b.y;
    return sqrt(delta_x * delta_x + delta_y * delta_y);
}

//Minimum value of water tax
double min(double a, double b, double c) {
    int t;
    t = (a < b) ? a : b;
    t = (t < c) ? t : c;
    return t;
}

//Merger Alignment
void Merge(DOT *arr , int f, int mid, int l)
{
    DOT temp[16];

    int first1 = f; int last1 = mid;
    int first2 = mid+1; int last2 = l;
    int index = first1;

    for(;(first1<=last1)&&(first2<=last2); ++index)
    {
        if(arr[first1].x < arr[first2].x)
        {
            temp[index] = arr[first1];
            ++first1;
        }
        else
        {
            temp[index] = arr[first2];
            ++first2;
        }
    }

    for(; first1 <= last1; ++first1, ++index)
        temp[index] = arr[first1];
    for(; first2 <= last2; ++first2, ++index)
        temp[index] = arr[first2];
    for(index = f; index<=l; ++index)
        arr[index] = temp[index];
}

void Mergesort(DOT * arr , int first, int last)
{
    if(first < last)
    {
        int middle = (first+last)/2;
        Mergesort(arr,first,middle);
        Mergesort(arr,middle+1,last);
        Merge(arr, first,middle,last);
    }
}

//ClosePair function
//Area is the area where the points are gathered, and start~end is the range of start and end based on the index.
//The points in the input area should be in ascending order based on the x-axis value.
double ClosestPair(DOT * Area, int start, int end) { 
    //Repeat temporary variable
    int i, j;
    //Minimum length of left area
    double left_side;
    //Minimum length of right area
    double right_side;
    //The length of two points divided in the middle
    double middle_side;
    //Index range difference = (actual count) - 1
    int D = (end-start);
    //distance region variable
    double dist;

    If (D < 2) // only two points exist,
        return distance(Area[start], Area[end]);
    Else { // If there are more than 3 points, divide
        If(D%2) { // if the number is odd
            left_side = ClosestPair(Area, start, (start + end)/2 );
            right_side = ClosestPair(Area, (start + end)/2 + 1 , end);
            dist = MIN(left_side, right_side);

            //Getting Intermediate Distance
            //Find the right
            i = 0;
            while(1){
                double mid_distance_right = distance(Area[start + (D/2) - 1], Area[start + (D/2) + i]);
                if(mid_distance_right < dist) i++;
                else break;
            }
            //Find the left
            j = 0;
            while(1){
                double mid_distance_left = distance(Area[start + (D/2) - j], Area[start + (D/2)]);
                if(mid_distance_left < dist) j++;
                else break;
            }

            middle_side = distance(Area[start + (D/2) - j], Area[start + (D/2) + i]);

            return min(left_side, right_side, middle_side);
        }
        else { // if the number is even
            left_side = ClosestPair(Area, start, start + (D/2));
            right_side = ClosestPair(Area, start + (D/2) + 1, end);
            dist = MIN(left_side, right_side);

            //Getting Intermediate Distance
            //Find the right
            i = 0;
            while(1){
                double mid_distance_right = distance(Area[start + (D/2)], Area[start + (D/2) + i]);
                if(mid_distance_right < dist) i++;
                else break;
            }
            //Find the left
            j = 0;
            while(1){
                double mid_distance_left = distance(Area[start + (D/2) - j], Area[start + (D/2) + 1]);
                if(mid_distance_left < dist) j++;
                else break;
            }

            middle_side = distance(Area[start + (D/2) - j], Area[start + (D/2) + i]);

            return min(left_side, right_side, middle_side);
        }        
    }
}

int main() {
    DOT Area[NUM_OF_DOTS] = {{10,15}, {5,15}, {20,3}, {6,1}, {9,7}, {15,9}, {8,15}, {20,14}, {17,13}, {16,11}, {7,12}, {10,10}, {1,19}, {8,8}, {30,9}, {22,4}};
    Mergesort (Area, 0, 15); //align
    printf("%lf",sqrt(ClosestPair(Area,0,15)));
    return 0;
}

This code appeared to be running normally because the value of 1.414214 was output Even if you change the position of the point, it continues to output the same value, 1.414214.

Can you check where it went wrong?

algorithm

2022-09-22 20:21

1 Answers

DOTArea[NUM_OF_DOTS] = {{130,145}, {15,153}, {420,35}, {66,14}, {93,71}, {458,1345, {201,119,148,138,127,12}, {14}, {349,14},} {349,148,13}, {16,153},}

If you change all the dots like this, you get a different answer. Perhaps the questioner left the two points that determine the closest distance between the dots and switched the other ones.

But there are only 16 moles, so I wonder why you worked so hard to squeeze them.

#include <stdio.h>
#include <math.h>
#define NUM_OF_DOTS 16

typedef struct{
    int y;
    int x;
} } DOT;

int main() {
    int i, j;
    double min_dist = 1234567890;
    DOT Area[NUM_OF_DOTS] = {{10,15}, {5,15}, {20,3}, {6,1}, {9,7}, {15,9}, {8,15}, {20,14}, {17,13}, {16,11}, {7,12}, {10,10}, {1,19}, {8,8}, {30,9}, {22,4}};

    for(i = 0 ; i < NUM_OF_DOTS - 1 ; i++)
    {
        for(j = i + 1 ; j < NUM_OF_DOTS ; j++)
        {
            double delta_x = Area[i].x - Area[j].x;
            double delta_y = Area[i].y - Area[j].y;
            double dist = sqrt(delta_x * delta_x + delta_y * delta_y);

            if(dist < min_dist)
                min_dist = dist;
        }
    }

    printf("%lf\n", min_dist);
    return 0;
}


2022-09-22 20:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.