Problem:
Given a current position and diagonally opposite coordinates of a rectangle. Find the minimum distance to reach border of rectangle from current position
Input Format:
String x y x1 y1 x2 y2 where x,y is current position, x1,y1 and x2,y2 are opposite coordinates of a rectangle.
Output Format:
Minimum distance
Minimum distance
Constraints:
none
none
Sample Input
1 3 -4 -4 5 5
Sample Output:
2
2
Explanations:-
Minimum distance is 1,3 to 1,5 ie 2
Solution:
@br=split(" ",$n);
$d=$br[0]-$br[2];
if($d<0)
{
$d=$d * -1;
}
$tmp=$br[0]-$br[4];
if($tmp<0)
{
$tmp=$tmp * -1;
}
if($d>$tmp)
{
$d=$tmp;
}
$tmp=$br[1]-$br[3];
if($tmp<0)
{
$tmp=$tmp * -1;
}
if($d>$tmp)
{
$d=$tmp;
}
$tmp=$br[1]-$br[5];
if($tmp<0)
{
$tmp=$tmp * -1;
}
if($d>$tmp)
{
$d=$tmp;
}
print "$d";
Tips:
Just find the distance from all coordinates and select minimum
No comments:
Post a Comment