|
Initially, we start off with a blank maze.
|
Queue: Empty. |
 |
|
|
We move to the "Right" out of the starting position.
We add [0, 2] to the tail of the queue, and mark [0, 2]
with an R.
|
Queue: { [0, 2] }
|
 |
|
|
We draw [0, 2] from the head of the queue. We are
only able to move Right into [1, 2]. We add [1, 2]
to the tail of the queue, and mark [1, 2] with an R.
|
Queue: { [1, 2] }
|
 |
|
|
We draw [1, 2] from the head of the queue. We
can move Down into [1, 3]. We add [1, 3] to the
tail of the queue and mark [1, 3] with a D.
|
Queue: { [1, 3] }
|
 |
|
|
We can also move Up from [1, 2] into [1, 1]. We add
[1, 1] to the tail of the queue and mark [1, 1] with
a U. (Notice that this ordering will become important
later.)
|
Queue: { [1, 3], [1, 1] }
|
|
We draw [1, 3] from the head of the queue. We can only move
Down into [1, 4]. We add [1, 4] to the tail of the queue
and mark [1, 4] with an D.
|
Queue: { [1, 1], [1, 4] }
|
 |
|
|
We draw [1, 1] from the head of the queue. We can only move
Right into [2, 1]. We add [2, 1] to the tail of the queue
and mark [2, 1] with an R.
|
Queue: { [1, 4], [2, 1] }
|
|
We draw [1, 4] from the queue. We can move Down into [1, 5].
We add [1, 5] to the tail of the queue and mark [1, 5] with a
D.
|
Queue: { [2, 1], [1, 5] }
|
 |
|
|
We can also move Right from [1, 4] into [2, 4]. We add [2, 4]
to the tail of the queue and mark [2, 4] with an R.
|
Queue: { [2, 1], [1, 5], [2, 4] }
|
|
We draw [2, 1] from the queue. We can only move Right into [3, 1].
We add [3, 1] to the tail of the queue and mark [3, 1] with an R.
|
Queue: { [1, 5], [2, 4], [3, 1] }
|
|
We draw [1, 5] from the queue. We can only move Down into [1, 6].
We add [1, 6] to the tail of the queue and mark [1, 6] with a D.
|
Queue: { [2, 4], [3, 1], [1, 6] }
|
 |
|
|
We draw [2, 4] from the queue. We can only move Right into [3, 4].
We add [3, 4] to the tail of the queue and mark [3, 4] with an R.
|
Queue: { [3, 1], [1, 6], [3, 4] }
|
|
We draw [3, 1] from the queue. We can only move Right into [4, 1].
We add [4, 1] to the tail of the queue and mark [4, 1] with an R.
|
Queue: { [1, 6], [3, 4], [4, 1] }
|
|
We draw [1, 6] from the queue. We can only move Right into [2, 6].
We add [2, 6] to the tail of the queue and mark [2, 6] with an R.
|
Queue: { [3, 4], [4, 1], [2, 6] }
|
 |
|
|
We draw [3, 4] from the queue. We can only move Up into [3, 3].
We add [3, 3] to the tail of the queue and mark [3, 3] with a U.
|
Queue: { [4, 1], [2, 6], [3, 3] }
|
|
We draw [4, 1] from the queue. We can only move Right into [5, 1].
We add [5, 1] to the tail of the queue and mark [5, 1] with an R.
|
Queue: { [2, 6], [3, 3], [5, 1] }
|
|
We draw [2, 6] from the queue. We can only move Right into [3, 6].
We add [3, 6] to the tail of the queue and mark [3, 6] with an R.
|
Queue: { [3, 3], [5, 1], [3, 6] }
|
 |
|
|
We draw [3, 3] from the queue. We can only move Right into [4, 3].
We add [4, 3] to the tail of the queue and mark [4, 3] with an R.
|
Queue: { [5, 1], [3, 6], [4, 3] }
|
|
We draw [5, 1] from the queue. We can only move Down into [5, 2].
We add [5, 2] to the tail of the queue and mark [5, 2] with a D.
|
Queue: { [3, 6], [4, 3], [5, 2] }
|
|
We draw [3, 6] from the queue. We can only move Right into [4, 6].
We add [4, 6] to the tail of the queue and mark [4, 6] with an R.
|
Queue: { [4, 3], [5, 2], [4, 6] }
|
 |
|
|
We draw [4, 3] from the queue. We can only move Right into [5, 3].
We add [5, 3] to the tail of the queue and mark [5, 3] with an R.
|
Queue: { [5, 2], [4, 6], [5, 3] }
|
|
We draw [5, 2] from the queue. We can't move anywhere! We
could've moved down into [5, 3] had we gotten here earlier, but
the other path got here first. Indeed, both paths would've been
the same length, but the one that got here first was earlier
alphabetically. We add nothing to the queue and mark nothing.
For clarity in the diagram, I marked this path purple, but the
algorithm itself does nothing.
|
Queue: { [4, 6], [5, 3] }
|
|
We draw [4, 6] from the queue. We can only move Right into
[5, 6]. We add [5, 6] to the tail of the queue and mark [5, 6]
with an R.
|
Queue: { [5, 3], [5, 6] }
|
 |
|
|
We draw [5, 3] from the queue. We can only move Down into
[5, 4]. We add [5, 4] to the tail of the queue and mark
[5, 4] with a D.
|
Queue: { [5, 6], [5, 4] }
|
|
We draw [5, 6] from the queue. We can only move Up into
[5, 5]. We add [5, 5] to the tail of the queue and mark [5, 5]
with a U.
|
Queue: { [5, 4], [5, 5] }
|
 |
|
|
We draw [5, 4] from the queue. We can't move anywhere!
As before, another path got here first. And again, in
this case, it's another path whose length would be the
same as this one, but this path got here first because it's
earlier alphabetically. We add nothing to the queue and
mark nothing.
|
Queue: { [5, 5] }
|
|
We draw [5, 5] from the queue. We can only move Right into
[6, 5]. We add [6, 5] to the tail of the queue and mark
[6, 5] with an R.
|
Queue: { [6, 5] }
|
 |
|
We draw [6, 5] from the queue. We can only move Right,
and that hits the finishing point. At this point, we
retrace our steps, adding our result letters to our
string in reverse. When we're done, we print out
RRDDDDRRRRUR.
|
 |