

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.
- Check each adjacent cell and it is not supposed to be a wall(#)
- Adjacent cell should not be out of bounds, for example cells on the boundary cannot go outside the labyrinth.
- 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.

Talking product sense with Ridhi
9 min AI interview5 questions

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

+1

Added you to tag list.

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

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

+1

+1

+1


Daily Series #2: Geeking out → Monte Carlo Simulations
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.
Imagine you have a very complex situation with varying degrees of randomness. How do you...


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...

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...