
- 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
Program to check robot can reach target position or not in Python
Suppose we have a robot, that is currently sitting in at position (0, 0) (Cartesian plane). If we have list of its moves that it can make, containing N(North), S(South), W(West), and E(East). We have to check whether it can reach at destination coordinate (x, y).
So, if the input is like moves = ['N','N','E','E','S'], (x,y) = (2,1), then the output will be True,
To solve this, we will follow these steps −
- temp_coord := [0,0]
- for each move in moves, do
- if move is same as "N", then
- temp_coord[1] := temp_coord[1] + 1
- otherwise when move is same as "S", then
- temp_coord[1] := temp_coord[1] - 1
- otherwise when move is same as "E", then
- temp_coord[0] := temp_coord[0] + 1
- otherwise when move is same as "W", then
- temp_coord[0] := temp_coord[0] - 1
- if move is same as "N", then
- return True when temp_coord[0] is same as coord[0] and temp_coord[1] is same as coord[1] otherwise false.
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, moves, coord): temp_coord = [0,0] for move in moves: if move == "N": temp_coord[1] += 1 elif move == "S": temp_coord[1] -= 1 elif move == "E": temp_coord[0] += 1 elif move == "W": temp_coord[0] -= 1 return temp_coord[0] == coord[0] and temp_coord[1] == coord[1] ob = Solution() moves = ['N','N','E','E','S'] coord = [2,1] print(ob.solve(moves, coord))
Input
['N','N','E','E','S'], [2,1]
Output
True
- Related Articles
- Program to check robot can reach target by keep moving on visited spots in Python
- Program to check we can reach leftmost or rightmost position or not in Python
- C++ code to check grasshopper can reach target or not
- Program to check we can reach at position n by jumping or not in Python
- Program to check we can update a list index by its current sum to reach target or not in python
- Program to check person can reach top-left or bottomright cell avoiding fire or not in Python
- Program to check whether robot is moving inside a bounded box or not in Python
- Program to check whether we can reach last position from index 0 in Python
- Program to find minimum steps to reach target position by a chess knight in Python
- Program to check we can spell out the target by a list of words or not in Python
- C++ code to count steps to reach final position by robot
- Program to check robbers can rob the vault or not in Python
- Program to check typed string is for writing target string in stuck keyboard keys or not in Python
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python

Advertisements