TopcoderArchiveVisit Topcoder
SRM25 May 2011
SRM 514

MagicalGirlLevelOneDivTwo

Problem statement, definition, constraints, and public examples.

Problem Statement

Magical Girls are girls who have magical powers. They fight against evil witches to protect the Earth.


You, one of the Magical Girls, are now at point (0, 0) on the plane. You found an evil witch at point (x, y), so you will attack her by Attack Magic.


The range of your Attack Magic is a square with side length 2d centered at your location. That is, if you are at point (u, v), the square which indicates the range of your magic has four vertices (u+d, v+d), (u-d, v+d), (u-d, v-d) and (u+d, v-d).


Before casting Attack Magic, you may move to any point in the plane. Return the minimum distance you have to move so that the evil witch is inside or on the edge of the square.

Definition

Class:
MagicalGirlLevelOneDivTwo
Method:
theMinDistance
Parameters:
int, int, int
Returns:
double
Method signature:
double theMinDistance(int d, int x, int y)
(be sure your method is public)

Notes

  • The distance between two points (x1, y1) and (x2, y2) is sqrt((x2-x1)^2 + (y2-y1)^2).
  • The returned value must have an absolute or relative error less than 1e-9.

Constraints

  • d will be between 1 and 500, inclusive.
  • x and y will each be between -500 and 500, inclusive.

Examples

  1. 5
    10
    10
    Returns: 7.0710678118654755
    Moving to (5, 5) achieves the minimum distance. The distance is sqrt(5*5 + 5*5) = 7.0710678118654755.
  2. 5
    10
    3
    Returns: 5.0
    Moving to (5, 0) achieves the minimum distance. The distance is sqrt(5*5 + 0*0) = 5.
  3. 5
    -8
    -6
    Returns: 3.1622776601683795
  4. 5
    3
    2
    Returns: 0.0
    The evil witch is already inside of the square.
  5. 24
    24
    -24
    Returns: 0.0
  6. 345
    -471
    333
    Returns: 126.0
Back to all problems