push adds to the toppop removes the top elementpeek inspects the top elementenqueue adds to the enddequeue removes from the frontfront inspects the next element| Structure | Rule | Best For | Typical Examples |
|---|---|---|---|
| Stack | LIFO | Backtracking, nested execution, undo | Browser history, recursion, maze exploration |
| Queue | FIFO | Sequential processing, fairness, scheduling | Message brokers, print queues, event handling |
An explorer robot has been sent to map a maze full of narrow passages, locked doors, and scattered keys. The robot's goal is to collect the necessary keys and reach the exit, avoiding dead ends and obstacles along the way.
The robot follows two fundamental rules:
1. If it needs to backtrack, it must follow the steps in reverse order β We use a stack (LIFO) to store movements.
2. If it picks up a key, it must be used in the order it was collected β We use a queue (FIFO) to manage the keys.
Your challenge is to implement the robot's movement logic, ensuring that it correctly explores the maze, collects and uses keys in the right order, and finds the exit.
S β . β K β . β D β . β E
β β β
. X . X .
β β β
. β . β K β . β DLegend:
- S = Robot's starting point
- E = Exit (final goal)
- K = Key (added to the queue when collected)
- D = Door (only opens if there is a key in the queue)
- X = Wall (impassable obstacle)
- . = Free path
You must implement the robot's movement logic using:
- A stack to store movements (allowing it to backtrack from dead ends).
- A queue to store collected keys and ensure doors are unlocked in the correct order.