📌  相关文章
📜  访问从源到最终顶点的每个坐标所覆盖的最小曼哈顿距离

📅  最后修改于: 2021-09-03 03:51:43             🧑  作者: Mango

给定一个坐标点数组 arr[]以及一个源和最终坐标点,任务是找到从源到最终顶点所覆盖的最小曼哈顿距离,使得该数组的每个点都恰好被访问一次。

例子:

方法:想法是使用排列组合来生成每个可能的排列移动到坐标,然后计算从数组的第一个坐标移动到最终坐标所覆盖的总曼哈顿距离,如果最终覆盖的距离小于到目前为止所覆盖的最小距离。然后更新覆盖的最小距离。
下面是上述方法的实现:

C++
// C++ implementation to find the
// minimum manhattan distance
// covered by visiting N co-ordinates
 
#include 
using namespace std;
 
// Class of co-ordinates
class pairs {
public:
    int x;
    int y;
};
 
// Function to calculate the
// manhattan distance between
// pair of points
int calculate_distance(pairs a,
                       pairs b)
{
    return abs(a.x - b.x) +
           abs(a.y - b.y);
}
 
// Function to find the minimum
// distance covered for visiting
// every co-ordinate point
int findMinDistanceUtil(vector nodes,
           int noOfcustomer, int** matrix)
{
    int mindistance = INT_MAX;
     
    // Loop to compute the distance
    // for every possible permutation
    do {
        int distance = 0;
        int prev = 1;
         
        // Computing every total manhattan
        // distance covered for the every
        // co-ordinate points
        for (int i = 0; i < noOfcustomer; i++) {
            distance = distance +
                       matrix[prev][nodes[i]];
            prev = nodes[i];
        }
         
        // Adding the final distance
        distance = distance + matrix[prev][0];
         
        // if distance is less than
        // minimum value than update it
        if (distance < mindistance)
            mindistance = distance;
    }while (
        next_permutation(
            nodes.begin(), nodes.end()
        ));
    return mindistance;
}
 
// Function to initialize the input
// and find the minimum distance
// by visiting every coordinate
void findMinDistance()
{
    int noOfcustomer = 1;
    vector coordinate;
    vector nodes;
    // filling the coordinates into vector
    pairs office, home, customer;
    office.x = 0;
    office.y = 0;
    coordinate.push_back(office);
    home.x = 5;
    home.y = 5;
    coordinate.push_back(home);
    customer.x = 1;
    customer.y = 1;
    coordinate.push_back(customer);
     
    // make a 2d matrix which stores
    // distance between two point
    int** matrix = new int*[noOfcustomer + 2];
     
    // Loop to compute the distance between
    // every pair of points in the co-ordinate
    for (int i = 0; i < noOfcustomer + 2; i++) {
        matrix[i] = new int[noOfcustomer + 2];
         
        for (int j = 0; j < noOfcustomer + 2; j++) {
            matrix[i][j] = calculate_distance(
                    coordinate[i], coordinate[j]);
        }
         
        // Condition to not move the
        // index of the source or
        // the final vertex
        if (i != 0 && i != 1)
            nodes.push_back(i);
    }
    cout << findMinDistanceUtil(
        nodes, noOfcustomer, matrix);
}
 
// Driver Code
int main()
{
    // Function Call
    findMinDistance();
    return 0;
}


输出:
10

性能分析:

  • 时间复杂度: O(N! * N)
  • 辅助空间: O(N 2 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live