Given an array **arr[]** of co-ordinate points and a source and final co-ordinate point, the task is to find the minimum manhattan distance covered from the source to the final vertex such that every point of the array is visited exactly once.

Manhattan Distance =

**Examples:**

Input:source = (0, 0), final = (100, 100)

arr[] = {(70, 40), (30, 10), (10, 5), (90, 70), (50, 20)}

Output:200

Input:source = (0, 0), final = (5, 5)

arr[] = {(1, 1)}

Output:10

**Approach:** The idea is to use permutation and combination to generate every possible permutation movements to the co-ordinates and then compute the total manhattan distance covered by moving from the first co-ordinate of the array to the final co-ordinate and If the final distance covered is less than the minimum distance covered till now. Then update the minimum distance covered.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// minimum manhattan distance ` `// covered by visiting N co-ordinates ` ` ` `#include <bits/stdc++.h> ` `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<` `int` `> 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 intialize the input ` `// and find the minimum distance ` `// by visiting every cordinate ` `void` `findMinDistance() ` `{ ` ` ` `int` `noOfcustomer = 1; ` ` ` `vector<pairs> cordinate; ` ` ` `vector<` `int` `> nodes; ` ` ` `// filling the coordinates into vector ` ` ` `pairs office, home, customer; ` ` ` `office.x = 0; ` ` ` `office.y = 0; ` ` ` `cordinate.push_back(office); ` ` ` `home.x = 5; ` ` ` `home.y = 5; ` ` ` `cordinate.push_back(home); ` ` ` `customer.x = 1; ` ` ` `customer.y = 1; ` ` ` `cordinate.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( ` ` ` `cordinate[i], cordinate[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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

10

**Performance Analysis:**

**Time Complexity:***O(N! * N)***Auxiliary Space:***O(N*^{2})

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of shortest distance on source to destination and back having at least a common vertex
- Find the original coordinates whose Manhattan distances are given
- Minimum distance to the corner of a grid from source
- Final direction after visiting every cell of Matrix starting from (0, 0)
- Pairs with same Manhattan and Euclidean distance
- Find the integer points (x, y) with Manhattan distance atleast N
- Find the maximum distance covered using n bikes
- Find the distance covered to collect items at equal distances
- Length of race track based on the final distance between participants
- Find minimum area of rectangle with given set of coordinates
- Check if incoming edges in a vertex of directed graph is equal to vertex itself or not
- Check if vertex X lies in subgraph of vertex Y for the given Graph
- Minimum possible final health of the last monster in a game
- Finding minimum vertex cover size of a graph using binary search
- Minimum distance between the maximum and minimum element of a given Array
- Minimum edges to reverse to make path from a source to a destination
- Minimum Sum of a pair at least K distance apart from an Array
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Minimum value of distance of farthest node in a Graph
- Minimum distance from a point to the line segment using Vectors

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.