Program to check whether robot is moving inside a bounded box or not in Python

Suppose we have a string s that represents the moves of a robot. The robot starts at position (0, 0) and faces north. We need to determine if the robot stays within a bounded area after repeatedly executing the move sequence.

Move Commands

The move string s contains these characters ?

  • "F" − move forward one unit in current direction
  • "L" − rotate 90 degrees left
  • "R" − rotate 90 degrees right

Algorithm Logic

If a robot returns to its starting position (0, 0) after executing the move sequence up to 4 times, it will stay bounded. This is because after 4 repetitions, any movement pattern will either cycle back to origin or continue infinitely.

Start (0,0) Bounded Robot Path Example

Implementation

def is_robot_bounded(s):
    # Direction vectors: North, East, South, West
    moves = [[0, 1], [1, 0], [0, -1], [-1, 0]]
    row, col = 0, 0  # Starting position
    direction = 0    # 0=North, 1=East, 2=South, 3=West

    # Execute move sequence up to 4 times
    for cycle in range(4):
        for move in s:
            if move == "F":
                row += moves[direction][0]
                col += moves[direction][1]
            elif move == "L":
                direction = (direction + 3) % 4  # Turn left
            elif move == "R":
                direction = (direction + 1) % 4  # Turn right
        
        # If robot returns to origin, it's bounded
        if row == 0 and col == 0:
            return True
    
    return False

# Test the function
moves = "FFRFRFFRF"
result = is_robot_bounded(moves)
print(f"Move sequence: {moves}")
print(f"Is robot bounded: {result}")
Move sequence: FFRFRFFRF
Is robot bounded: True

How It Works

The algorithm tracks the robot's position and direction after each move sequence execution. The key insight is that if the robot doesn't return to origin within 4 cycles, it will move infinitely in some direction.

Direction Encoding

  • 0 − North (up)
  • 1 − East (right)
  • 2 − South (down)
  • 3 − West (left)

Step-by-Step Example

def trace_robot_path(s):
    moves = [[0, 1], [1, 0], [0, -1], [-1, 0]]
    row, col = 0, 0
    direction = 0
    
    print(f"Move sequence: {s}")
    print(f"Starting at ({row}, {col}), facing North")
    
    for i, move in enumerate(s):
        if move == "F":
            row += moves[direction][0]
            col += moves[direction][1]
            print(f"Step {i+1}: Move forward to ({row}, {col})")
        elif move == "L":
            direction = (direction + 3) % 4
            directions = ["North", "East", "South", "West"]
            print(f"Step {i+1}: Turn left, now facing {directions[direction]}")
        elif move == "R":
            direction = (direction + 1) % 4
            directions = ["North", "East", "South", "West"]
            print(f"Step {i+1}: Turn right, now facing {directions[direction]}")
    
    print(f"Final position: ({row}, {col})")
    return row == 0 and col == 0

# Trace the example
trace_robot_path("FFRFRFFRF")
Move sequence: FFRFRFFRF
Starting at (0, 0), facing North
Step 1: Move forward to (0, 1)
Step 2: Move forward to (0, 2)
Step 3: Turn right, now facing East
Step 4: Move forward to (1, 2)
Step 5: Turn right, now facing South
Step 6: Move forward to (1, 1)
Step 7: Move forward to (1, 0)
Step 8: Turn right, now facing West
Step 9: Move forward to (0, 0)
Final position: (0, 0)

Conclusion

A robot stays bounded if it returns to the origin (0, 0) within 4 executions of the move sequence. The algorithm efficiently determines this by simulating the robot's movement and checking for the return condition after each cycle.

Updated on: 2026-03-26T16:49:03+05:30

336 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements