← 返回 meta 的题目列表Robot Room Cleaner Variant
类型:online_judge
You need to design an algorithm to control a robot to move in a room of unknown size. The robot starts from an arbitrary point within a room. You cannot access a map of the room and the boundaries are unknown. The robot can interact with the environment using the following API:
move(): Moves the robot forward if there is no obstacle, returning True; otherwise, returns False.
turnLeft() / turnRight(): Rotates the robot left or right by 90 degrees.
isTarget(): Returns True if the current position is the target; otherwise, returns False.
The objective is to find the target position from the starting position and stop once it's found. The robot does not need to traverse or clean the entire room. You need to keep track of visited positions to prevent revisiting them. When unable to move forward, the robot should effectively backtrack to a previous position.
Requirements:
Define the starting point as (0, 0).
Use a directional array to represent relative movement:
0: up
1: right
2: down
3: left
Use visited = set() to store positions that the robot has already visited.
Test Cases:
The starting position is the same as the target position, return immediately.
There are multiple obstacles and dead ends; ensure the algorithm can dynamically avoid and backtrack correctly.
Test in a finite-sized room to observe time and space complexity performance.