سؤال

I need some help with the math behind an assignment i was given. The problem was to calculate the distance from the ant to the candy. The candy is always on top of the box and the ant can be anywhere but the sides. The ant can craw on the top and the sides but CANNOT fly. They give you the coordinates of the ant and the candy as 6 numbers in a row. The first 3 are the ant's the next 3 are the candy's. The problem i am having is on the 3rd sample input (0 0 0 5 4 3.0). How do they get 8.60 units as the answer? When the distance up is 3 and the distance to the corner is 6.40 so the total should be 9.40. My guess is they cut the corner while going up but im not sure how to make a formula for the to find the shortest length like that. Thanks for your help :D if you need more information just ask.

The box is (5,4,3) in x,z,y format.

Sample Input:                       
3 1 3 3 3 3            
2.25 0 2 2.5 2 3          
0 0 0 5 4 3.0   
0 4 3 5 0.0 3        
5 0 3 5 4.00 3           

Sample Output:               
Shortest distance = 2.00 units            
Shortest distance = 3.01 units        
Shortest distance = 8.60 units             
Shortest distance = 6.40 units            
Shortest distance = 4.00 units
هل كانت مفيدة؟

المحلول

First, this is quite clearly in the wrong area, as gap_j has pointed out. That said, the answer lies in calculus -- you'll have to set the derivative equal to zero and identify the minimum(s). Based on the dimensions you've provided and an inference or two, the box is 5 units on the x-axis, 4 units on the y-axis, and 3 units on the z-axis. This means that, as you note, the shortest distance by traveling first up the z-axis would be 9.40 units:

p(z) = z + sqrt(x2 + y2)

There are two other options, with respect to traveling directly along an axis first; x-first and y-first:

p(x) = x + sqrt(y2 + z2)

p(y) = y + sqrt(x2 + z2)

These paths have values of 10 (exactly) and 9.83, respectively.

In order to achieve the p = 8.6 given in the problem, there would have to be some distance a along the x-axis such that:

p(a) = sqrt((x - a)2 + z2) + sqrt(y2 + a2)

else some distance b along the y-axis such that:

p(b) = sqrt((y - b)2 + z2) + sqrt(x2 + b2)

and the values p(a) or p(b) must be less than traveling directly along an axis. There are myriad such values for p(a).

Since this is presumably a homework problem for a calculus class, I'll leave finding the derivative to you, but the formulas are as provided. There is only one variable, so this shouldn't be particularly difficult. These can be generalized, of course, and it's a fairly simple task to calculate the results and identify the shorter path.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top