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

**Approach: **Follow the steps below to solve the problem:

- Initially robot is at
**(0, 0)**facing north. - Assign variables to keep track of the current position and direction of the robot after each step.
- Store the coordinates of obstacles in a HashMap.
- Make
**2**arrays**(dx[], dy[])**and store all possible movements in**x**and**y**coordinates according to the change in direction. - If direction change is encountered, change the present direction referring to the 2 arrays.
- Otherwise, keep on moving in the same direction until a direction change is encountered if no obtacles occur in between.
- 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) ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Distance traveled by Hour and Minute hand in given Time interval
- Distance Traveled by Two Trains together in the same Direction
- Pairs with same Manhattan and Euclidean distance
- Generate a String from given Strings P and Q based on the given conditions
- Construct the Cypher string based on the given conditions
- Maximum items that can be bought from the cost Array based on given conditions
- Maximum Sum possible by selecting X elements from a Matrix based on given conditions
- Maximum Count of pairs having equal Sum based on the given conditions
- Sum of indices of Characters removed to obtain an Empty String based on given conditions
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Generate a string from an array of alphanumeric strings based on given conditions
- Range sum queries based on given conditions
- Queries to search for an element in an array and modify the array based on given conditions
- Count permutations of all integers upto N that can form an acyclic graph based on given conditions
- Minimum Sum of Euclidean Distances to all given Points
- Number of Triangles that can be formed given a set of lines in Euclidean Plane
- Euclidean algorithms (Basic and Extended)
- C Program for Basic Euclidean algorithms
- Find HCF of two numbers without using recursion or Euclidean algorithm
- Program to calculate Percentile of a student based on rank

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.