TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

MovinAround

Problem statement, definition, constraints, and public examples.

Problem Statement

You are initially standing at xy-coordinate (xinit,yinit) facing in the positive y-direction. A list of commands are given to you. Each command will be one of (quotes for clarity):
  • "Turn Right" : Turn 90 degrees to the right (i.e. if up you will be facing right; if right you will be facing down...)
  • "Move Forward" : Move 1 unit in the direction you are facing.
Note, x-coordinates increase to the right, and y-coordinates increase upward. Each command should be followed one after the other. You will return the maximum distance from your initial location to the location you are at after any of the commands. Suppose you were at position (xcurr,ycurr) after executing one of the commands. The distance from this location to the inital location is given by:
	Sqrt( (xcurr-xinit)^2 + (ycurr-yinit)^2) 
where Sqrt is the square root function, and ^ denotes exponentiation. To reiterate, you will return the maximum of any of these distances which are calculated after each command. See examples for further clarification.

Definition

Class:
MovinAround
Method:
distance
Parameters:
String[], int, int
Returns:
double
Method signature:
double distance(String[] moves, int xinit, int yinit)
(be sure your method is public)

Constraints

  • moves will contain between 1 and 50 elements inclusive
  • Each element of moves will be one of (quotes for clarity): "Move Forward", "Turn Right".
  • xinit will be between -100 and 100 inclusive
  • yinit will be between -100 and 100 inclusive

Examples

  1. {"Move Forward"}
    0
    0
    Returns: 1.0
    At the begining you are at (0,0). After moving forward you are at (0,1). The maximum distance is thus 1.
  2. {"Move Forward","Turn Right","Move Forward"}
    0
    0
    Returns: 1.4142135623730951
    Your path looks like (0,0)->(0,1)->(1,1) (1,1) is the furthest from the initial point so you return its distance.
  3. {"Move Forward",
    "Turn Right","Turn Right",
    "Move Forward"}
    10
    10
    Returns: 1.0
    Your path looks like: (10,10)->(10,11)->(10,10) The maximum distance is from point (10,11) so you return 1.
  4. {"Move Forward","Turn Right",
     "Move Forward","Move Forward","Turn Right",
     "Move Forward","Move Forward","Move Forward","Move Forward","Turn Right"}
    99
    44
    Returns: 3.605551275463989
Back to all problems