SquishyQuokka
SquishyQuokka

Daily Series #3: Geeking out → The Labyrinth Problem

I will continue with this series for people who like this kind of content, drop "+1" in the chat and I will tag you the next time I post content.

Today, I was in the mood to go through some old CS problems I had solved a few years back.

The problem is described in cses.fi [ https://cses.fi/problemset/task/1193 ] and is as follows:

You are given a map of a labyrinth, and your task is to find a path from start to end. You can walk left, right, up and down.

𝗜𝗻𝗽𝘂𝘁𝘀: You are given n and m: the height and width of the map. Then there are n lines of m characters describing the labyrinth. Each character is . (floor), # (wall), A (start), or B (end). There is exactly one A and one B in the input.

𝗢𝘂𝘁𝗽𝘂𝘁: Revert with "YES" if a path is possible. Revert with "NO" if no path is possible. If "YES" also mention the path taken to reach B from A as L(left), R (right), U (up), and D (down).

How do you solve a problem like this?

Solution Approach: You traverse through the input and figure out where A and B lie within the input as (i,j) coordinates. Then, we do something called flood fill approach, where we start at A and then we update moves with BFS until we reach B. (L/D/R/U)

Now, what we need to check next is whether it violates some assumptions.

  1. Check each adjacent cell and it is not supposed to be a wall(#)
  2. Adjacent cell should not be out of bounds, for example cells on the boundary cannot go outside the labyrinth.
  3. Lastly we need to track that we have not already visited that cell before.

If none of these cases are violated then we proceed to move in that direction. Then we apply BFS again and check for violation of these cases. All the while we maintain whether we visited that cell before and the path we have taken from A.

We stop when we reach B's location. Now what happens if we don't reach B? Since, we are applying BFS we will end up reaching all cells except B. We would have completed entire traversal of the labyrinth yet no B in sight.

Post image
14mo ago
Talking product sense with Ridhi
9 min AI interview5 questions
Round 1 by Grapevine
SquishyQuokka
SquishyQuokka
Gojek14mo

Tag List: @Sherlock007 @BladeRunner007 @TheOatmeal

SquishyQuokka
SquishyQuokka
Gojek14mo

@JadeArgent @potatomato @BiryaniEnthu

SquishyQuokka
SquishyQuokka
Gojek14mo

@Elon_Musk @ElonMast @Rhombus

FloatingBiscuit
FloatingBiscuit

Theres a great video by veritasium on use of flood fill algo in pathfinding robots which is quite relevant to this topic.

SwirlyPanda
SwirlyPanda

I was just about to comment this

SwirlyPretzel
SwirlyPretzel
Google14mo

I was just about to comment this too. The labyrinth problem seems very similar to RoboMaze solving bots. Can anyone drop the link?

GoofyDonut
GoofyDonut

Remember when Labyrinth was a famous mobile game!

SquishyQuokka
SquishyQuokka
Gojek14mo

Hahahah and also a famous singer

DizzySushi
DizzySushi

+1

SquishyQuokka
SquishyQuokka
Gojek14mo

Added you to tag list.

PerkyPancake
PerkyPancake

Nice read, brings back the memories of solving these kind of algorithmic questions during college time.

SquishyQuokka
SquishyQuokka
Gojek14mo

@Vegetaa Thise were the days man. I loved that era.

PeppyBanana
PeppyBanana

+1

WobblyLlama
WobblyLlama

+1

SillyDonut
SillyDonut
TCS14mo

+1

Discover more
Curated from across
Software Engineers
by GoofyBobaCred

How to make DS/Algo Prep fun?

I have always wanted to prepare DS/Algo like a pro, but have barely survived with my basic skills of that. How can I do it the right professional way, in a way that makes SDE interviews for Google level easy. I have a problem of not bei...

Misc
Misc10mo
by FluffyUnicornSoftware Engineer

Need your Experience Takeaways

I am a recent graduate with a BTECH CSE degree working at a very early age start-up as software developer this seems very normal but now that's a catch I am working from past 5 months and now I am started feeling stuck and overwhelmed li...