LEARNING JOURNAL

I created this learning journal to practice writting and to help me learn by writting everything down. Source code here

2/21/2024

Walls and Gates - Breadth-First Search

THE PROBLEM

This is the breadth-first search approach for this problem. First, it will add all gates to an array, then add null at the end. After that, it will use a pointer to examine each array's element, from start to end after setting a counter to 0. For each element, it will assign the current value of the counter to the cell then determine if its neighbor is an empty room. If it is, the neighbor will be added to the arr if its value is greater than the counter.

The catch here is that there will be time when the neighbor of a node is already visited and has the value less than its value + 1, if we don't take care of it, it will be added to the arr and later on, will be assigned to the greater value, which is incorrect.

Revised by ChatGPT

This is a breadth-first search approach to the problem. The algorithm starts by adding all gates to an array and appending null at the end. It then uses a pointer to iterate through each element of the array, starting with a counter set to 0. For each element, it assigns the current value of the counter to the cell and check if its neighbors are empty rooms. If a neighbor is an empty room and its value is greater than the counter, it is added to the array.

A key consideration is that a neighbor of a node may have already been visited and assigned a value less than its current value plus 1. To prevent incorrect assignments, the algorithm ensures that only neighbors with values greater than the counter are added to the array.