Determining the distances from the points is trivial. Your problem is to determine the points from the distances, which is considerably more challenging. Note that if there is a solution, there will always be a symmetrical solution (for instance, 0, 2, 5, 8 is also a solution above). You only need to determine one solution, if such a solution exists, or signal that there is no solution.
Also, you will need the logical equivalent of findMax and deleteMax. You would expect back and pop_back to work, but they are not defined. Instead, you can write routines (note that the parameter to erase is an iterator):
template <class Object>
const Object & findMax( const multiset<Object> & m )
{
return *(--m.end( ) );
}
template <class Object>
void deleteMax( multiset<Object> & m )
{
m.erase( --m.end( ) );
}
// Driver routine that can be called from main.
// It is passed the set of distances (which is an input parameter).
// If these distances represent a solution, the solution will
// be placed in points, and solve returns true. Otherwise
// solve returns false, and the contents of points is undefined.
// 14 NON BLANK LINES OF CODE
bool solve( const multiset<int> & dist, multiset<int>
& points )
{
points.clear( );
// Initialize points
multiset<int> d = dist; // Make
a one-time copy of dist
// Find the two maximum values in dist.
// Make sure their difference is also in dist.
// If not, return false.
// Otherwise, remove these three values from
dist, and add
// three entries to points (0, max#2, max#1).
// Try to place all the remaining points.
return solveRecursive( d, points );
}
// Recursive routine to place remaining points.
// dist contains the distances. It is passed by copy so
// we can make changes to the copy (see EXTRA CREDIT).
// The contract of solveRecursive is identical to solve.
// solveRecursive calls the three parameter helper, below
// to handle the identical logic involved in trying to
// place a point at either distMax or pointsMax-distMax.
// 6 NON BLANK LINES OF CODE
bool solveRecursive( multiset<int> dist, multiset<int> &
points )
{
// Handle a base case.
// Hint: What is the most trivial case for which
you can return true?
// find maximum item in dist (distMax),
// and then find maximum item in points (pointsMax).
// You will want to try to place a point either
at
// distMax or pointsMax-distMax.
// Call a helper (solveRecursive, with three
parameters) twice.
// If either returns true, we return true. If
both return false
// we return false.
}
// Three parameter helper.
// Attempts to add a new point (pointToAdd) into points.
// If the attempt succeeds, we return true, and points has the
final answer.
// Otherwise, we return false, and points is undefined.
// See below for EXTRA CREDIT.
// 14 NON BLANK LINES OF CODE
bool solveRecursive( multiset<int> dist, multiset<int> &
points, int pointToAdd )
{
// Attempt to use it at location pointToAdd
// There must be a matching distance for each
// value in points && the recursive
call must succeed.
// So...
// Scan through points; at each step, compute
// a distance from pointsToAdd, and remove it
from dist.
// If the distance is not present, you can return
false.
// If we get here, all distances were present.
// Add pointToAdd to points, and then make a
call to
// solveRecursive (which one??) to place the
remaining points.
// If the call to solveRecursive returns true,
then we return true.
// Other wise we return false AFTER removing
pointToAdd from
// points (recall it was added above). Be careful
which erase
// you use.
}