191DG
191DG
A pizza delivery man wants to go home by delivering all ordered pizzas throughout
his way home. He wanted to go with the shortest path. Now help him by finding the
shortest path.
This man has to deliver 8 pizzas around the city. All the locations are given by the
coordinates X and Y including the location of his restaurant and home so that you
can find the distance between any two places.
Input: There are only 10 lines of input. Each line contains two integer X and Y for a
location. First-line and the last(10th)-line will contain the location of the restaurant
and home respectively.
Output:
1. The value of the minimum distance.
2. The sequence of the coordinates.
Sample Input:
18 0
10 -75
-45 84
50 45
-70 -55
75 60
-89 2
87 -17
-40 10
20 15
Sample output:
The minimum distance: 611
The shortest path: 18 0, -40 10, -45 84, -89 2, -70 -55, 10 -75, 87 -17, 75 60, 50 45, 20
15.
Hints
Two-point Distance.
To find the distance between any two places you can simply use this function.
int distance (int x1, int y1, int x2, int y2) {
int d=pow(x1-x2,2) +pow(y1-y2,2);
return sqrt(d);
}
Total distance of a path.
Use a for loop to complete this function to calculate the total distance of a path.
Here path is 2D array containing the location’s coordinates sequentially. If this array
contains five locations like 0, 1, 2, 3, 4 then you can sum the distances like ‘’0 to 1 + 1
to 2+ 2 to 3+ 3 to 4’’.
int pathCost(int arr[10][2], int size)
{ // this function is not complete.
int cost=0;
return cost;
}
Complexity.
Think about the previous example for such five locations the source and destination
remain unchanged. So, you need to shuffle the places between the source and
destination that means you need to shuffle 1 2 3 only.
Then the all-possible path will be 01234, 01324, 02134, 02314, 03124, 03214.
So, for five locations it will take 3! Steps. For N, it will take (N-2)! Steps.
Test case.
• For a large number of input it is almost impossible to calculate for a human. So,
test your program within a small (N<=5) number of inputs.
• For large number you can create test case using simple graph which shortest
path can be determined by watching the graph only, without calculation [fig. 1-
3].
Fig. 1
Suppose 40, 50 is the source and 20, 60 distance then you can determine the
solution by adding point clockwise [ Fig. 2].
Fig. 2
Solution is given in figure 3.
fi
Fig. 3
Graph link: https://www.desmos.com/calculator/blz6aahxgc