So what you, basically, have is something like...
+----+----+
Entrance -> | R1 | R4 |
+----+----+
| R2 | R3 | -> Exit
+----+----+
You need to find your way to R3
(where the exit is)
So, we can look at the problem, something like this.
We start at the entrance
+--------------+-----------------+------+---------+
| Current Room | Available Exits | Path | History |
+--------------+-----------------+------+---------+
| Entrance | R1 | | |
+--------------+-----------------+------+---------+
Add the current room to the history
+--------------+-----------------+------+----------+
| Current Room | Available Exits | Path | History |
+--------------+-----------------+------+----------+
| Entrance | R1 | | Entrance |
+--------------+-----------------+------+----------+
Does it contain Exit
? No, then let's find a room to goto, in this case, R1
is our only choice, let's got to R1
. Add Entrance
to our path
+--------------+------------------+----------+----------+
| Current Room | Available Exits | Path | History |
+--------------+------------------+----------+----------+
| R1 | Entrance, R2, R4 | Entrance | Entrance |
+--------------+------------------+----------+----------+
Does it contain Exit
? No. Okay, we need to remove all rooms we've already visited. That leaves us with R2
and R4
, doesn't matter which we pick.
Add the current room to the history and the path
+--------------+------------------+-------------+-------------+
| Current Room | Available Exits | Path | History |
+--------------+------------------+-------------+-------------+
| R1 | Entrance, R2, R4 | Entrance,R1 | Entrance,R1 |
+--------------+------------------+-------------+-------------+
And let's go to R4
+--------------+-----------------+-------------+-------------+
| Current Room | Available Exits | Path | History |
+--------------+-----------------+-------------+-------------+
| R4 | R1, R3 | Entrance,R1 | Entrance,R1 |
+--------------+-----------------+-------------+-------------+
Does it contain Exit
? No. We've already been too R1
, so that leaves us with R3
.
Add R4
to our path and history
+--------------+-----------------+----------------+----------------+
| Current Room | Available Exits | Path | History |
+--------------+-----------------+----------------+----------------+
| R4 | R1, R3 | Entrance,R1,R4 | Entrance,R1,R4 |
+--------------+-----------------+----------------+----------------+
And let's go to R3
+--------------+-----------------+----------------+----------------+
| Current Room | Available Exits | Path | History |
+--------------+-----------------+----------------+----------------+
| R3 | R2, R4, Exit | Entrance,R2,R4 | Entrance,R2,R4 |
+--------------+-----------------+----------------+----------------+
Does it contain Exit
? YES!!!
Add it to our path and lets scamper!!
+--------------+-----------------+---------------------+---------------------+
| Current Room | Available Exits | Path | History |
+--------------+-----------------+---------------------+---------------------+
| R3 | R2, R4, Exit | Entrance,R2,R4,Exit | Entrance,R2,R4,Exit |
+--------------+-----------------+---------------------+---------------------+
So, your path through the "maze" went Entrance -> R2 -> R4 -> Exit
and we didn't even need to backtrace!
Now, there is a pattern to the above workflow, see if you can pick out. Try choosing a different room each time and see where it takes you.
Technically, you don't need the history, as you are unlikely to ever back track, but if the maze becomes more complex, it will come in handy, because if you do backtrace, you'd pop the last element off the path, but the history will remind you not to go that way again ;)
Honestly, I think desk checking is so underrated as a concept, my wife gets furious with me because I have dozens of notes books laying around, all with different scribblings of different ideas in them, all unrelated, so I need a note book for each separate idea