# Minimum Manhattan distance covered by visiting every coordinates from a source to a final vertex

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})

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- 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
- Minimum possible final health of the last monster in a game
- Finding minimum vertex cover size of a graph using binary search
- Minimum edges to reverse to make path from a source to a destination
- Minimum value of distance of farthest node in a Graph
- Find integral points with minimum distance from given set of integers using BFS
- Minimum distance from a point to the line segment using Vectors
- Find the minimum sum of distance to A and B from any integer point in a ring of size N
- Minimum cost path from source node to destination node via an intermediate node
- Number of ways to reach (M, N) in a matrix starting from the origin without visiting (X, Y)
- Find a point such that sum of the Manhattan distances is minimized
- Total number of unit cells covered by all given Rectangles

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.