# Calculate the Square of Euclidean Distance Traveled based on given conditions

Given an array commands[], consisting of signed integers denoting distance and direction to be travelled along with the coordinates, and array obstacles[] denoting the coordinates which cannot be accessed, the task is to find the square of the square of maximum Euclidean distance that can be travelled started from the origin (0, 0) and facing north, following the commands specified in the sequence as in the commands[] array, of the following three types:

• -2: Turn left by 90 degrees.
• -1: Turn right by 90 degrees.
• 1<= X <= 9: Move forward by X units.

Examples:

Input: commands[] = {4, -1, 4, -2, 4}, obstacles[] = {{ 2, 4 }}
Output: 65
Explanation:
Step 1: (0, 0) -> (0, 4)
Step 2: (0, 4) -> (1, 4)
Step 3 and 4:
Obstacles
Step 5: (1, 4) -> (1, 8)

Input: commands[] = {4, -1, 3}, obstacles[] = {}
Output: 25

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Follow the steps below to solve the problem:

1. Initially robot is at (0, 0) facing north.
2. Assign variables to keep track of the current position and direction of the robot after each step.
3. Store the coordinates of obstacles in a HashMap.
4. Make 2 arrays(dx[], dy[]) and store all possible movements in x and y coordinates according to the change in direction.
5. If direction change is encountered, change the present direction referring to the 2 arrays.
6. Otherwise, keep on moving in the same direction until a direction change is encountered if no obtacles occur in between.
7. Finally, calculate the square of the x and y coordinates.

Below is the implementation of above approach:

## Python3

 `# Python Program to implement ` `# the above approach ` `def` `robotSim(commands, obstacles): ` ` `  `    ``# Possible movements in x coordinate. ` `    ``dx ``=` `[``0``, ``1``, ``0``, ``-``1``] ` ` `  `    ``# Possible movements in y coordinte. ` `    ``dy ``=` `[``1``, ``0``, ``-``1``, ``0``] ` ` `  `    ``# Initialise position to (0, 0). ` `    ``x, y ``=` `0``, ``0` ` `  `    ``# Initial direction is north. ` `    ``di ``=` `0` ` `  `    ``# Put all obstacles into hashmap. ` `    ``obstacleSet ``=` `set``(``map``(``tuple``, obstacles)) ` ` `  `    ``# maximum distance ` `    ``ans ``=` `0` ` `  `    ``# Iterate commands. ` `    ``for` `cmd ``in` `commands: ` `         `  `        ``# Left direction ` `        ``if` `cmd ``=``=` `-``2``:  ` `            ``di ``=` `(di``-``1``) ``%` `4` `         `  `        ``# Right direction ` `        ``elif` `cmd ``=``=` `-``1``: ` `            ``di ``=` `(di ``+` `1``) ``%` `4` `         `  `        ``# If no direction changes ` `        ``else``: ` `            ``for` `i ``in` `range``(cmd): ` `                ``# Checking for obstacles. ` `                ``if` `(x ``+` `dx[di], y ``+` `dy[di]) \ ` `                ``not` `in` `obstacleSet: ` `                     `  `                    ``# Update x coordinate ` `                    ``x ``+``=` `dx[di]  ` `                     `  `                    ``# Update y co ordinate ` `                    ``y ``+``=` `dy[di]  ` `                     `  `                    ``# Updating for max distance ` `                    ``ans ``=` `max``(ans, x ``*` `x ``+` `y ``*` `y) ` `    ``print``(ans) ` ` `  ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``commands ``=` `[``4``, ``-``1``, ``4``, ``-``2``, ``4``] ` `    ``obstacles ``=` `[[``2``, ``4``]] ` `    ``robotSim(commands, obstacles) `

Output:

```65
```

Time Complexity: O(N)
Auxiliary Space: O(N + M), where M is the length of obstacle array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.