Mr. X has to deliver software to N customers. From the office, he is going to visit all the customers and then return to his office. Each location of the office and the customers is given in the form of integer coordinates (x, y) (-1<x<500, -1<y<500). The distance between two arbitrary locations (x1, y1) and (x2, y2) is computed by |x1-x2| + |y1-y2|, where |x| denotes the absolute value of x; for instance, |3|=|-3|=3. The locations of the office and the customers are all distinct. You should plan an optimal way to visit all the N customers and return to his office.
You are given the locations of the office and the customers; the number of customers is in the range of 1 to 100. Write a program that, starting at the office, finds a (the) shortest path visiting all the customers and returning to his office. Your program only has to report the distance of a (the) shortest path.
[Constraints]
1<N<100. Each location (x, y) is in a bounded grid, -1<x<500, -1<y<500, and x, y are integers.
[Input]
Each test case consists of two lines;Â the first line has N, the number of the customers and the following line enumerates the locations of the office and the customers in sequence. Each location consists of the coordinates (x, y), which is represented by âx yâ.
[Output]
Each line outputs the distance of a (the) shortest path. Each line looks like â#x answerâ where x is the index of a test case. â#xâ and âanswerâ are separated by a space.
Examples:
Input :
In the first test case, the locations of the office are (0, 0) and the locations of the customers are (70, 40),
(30, 10), (10, 5), (90, 70), (50, 20).
5 (Starting test case #1)
0 0 70 40 30 10 10 5 90 70 50 20
Output :
#1 320
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
10 Oct, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...