
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Walking Robot Simulation in Python
Suppose there is a robot on an infinite grid that starts at point (0, 0). It is facing towards north direction. Now the robot can receive one of three possible types of commands −
- -2 to turn left 90 degrees
- -1 to turn right 90 degrees
- any value from 1 to 9 to move forward x units
- There are some grid squares that are obstacles.
We also have another array called obstacle, this indicates the i-th obstacle is at grid point (obstacles[i][0], obstacles[i][1]), If the robot wants to move onto them, the robot stays on the previous grid square instead.
We have to find the square of the maximum Euclidean distance that the robot will be from the origin.
So, if the input is like commands = [4,-1,4,-2,4], obstacles = [[2,4]], then the output will be 65, as robot will be stuck at (1, 4) before turning left and going to (1, 8).
To solve this, we will follow these steps −
- position_offset := [(0, 1) ,(1, 0) ,(0, -1) ,(-1, 0) ]
- initialize x, y, direction, max_distance as 0
- for each command in commands, do
- if command is same as -2, then
- direction :=(direction - 1) mod 4
- otherwise when command is same as -1, then
- direction :=(direction + 1) mod 4
- otherwise,
- (x_off, y_off) :=position_offset[direction]
- while command is non-zero, do
- if (x + x_off, y + y_off) not in obstacles, then
- x := x + x_off
- y := y + y_off
- command := command - 1
- if (x + x_off, y + y_off) not in obstacles, then
- max_distance = maximum of max_distance, x^2 + y^2
- if command is same as -2, then
- return max_distance
Let us see the following implementation to get better understanding −
Example
class Solution: def robotSim(self, commands, obstacles): position_offset = [(0, 1), (1, 0), (0, -1), (-1, 0)] obstacles = set(map(tuple, obstacles)) x, y, direction, max_distance = 0, 0, 0, 0 for command in commands: if command == -2: direction = (direction - 1) % 4 elif command == -1: direction = (direction + 1) % 4 else: x_off, y_off = position_offset[direction] while command: if (x + x_off, y + y_off) not in obstacles: x += x_off y += y_off command -= 1 max_distance = max(max_distance, x**2 + y**2) return max_distance ob = Solution() print(ob.robotSim([4,-1,4,-2,4],[[2,4]]))
Input
[4,-1,4,-2,4],[[2,4]]
Output
65
- Related Articles
- Basics of Discrete Event Simulation using SimPy in Python
- Squirrel Simulation in C++
- Simulation Studies in Psychology
- Dice Roll Simulation in C++
- Robot Bounded In Circle C++
- Program to check robot can reach target position or not in Python
- Robot Return to Origin in C++
- Simulation of 4-bit ALU
- What is Simulation Analysis in Capital Budgeting?
- Application of Cloud-Based Simulation in Scientific Research
- Simulation of 8 to 1 multiplexer
- Position of robot after given movements in C++
- Program to check robot can reach target by keep moving on visited spots in Python
- Program to check whether robot is moving inside a bounded box or not in Python
- Program to count how many blocks are covered k times by walking in Python
