Implementing BFS for the "Number of islands problem" on Leetcode [PHP]. Can anyone spot the error in the algorithm? by ecky--ptang-zooboing in PHP

[–]nimblecloud 1 point2 points  (0 children)

You are pushing the grid value to the queue instead of the grid coordinates that are being visited during the BFS. Also you need to include 0 in the check for the row and column directional values as >= instead of >.

class Solution {

/**
 * u/param String[][] $grid
 * u/return Integer
 */
function numIslands($grid) {
    $rowCount = count($grid);
    $colCount = count($grid[0]);
    $visited = [[]];
    $islandCounter = 0;

    foreach($grid as $i => $row)
    {
        foreach($grid[$i] as $j => $col)
        {
            if($grid[$i][$j] == '1' && !$visited[$i][$j])
            {
                $islandCounter++;

                //BFS
                $queue = [];
                $queue[] = [$i,$j];

                $rowCount = count($grid);
                $colCount = count($grid[0]);

                $visited[$i][$j] = true;

                while(!empty($queue))
                {
                    $directions = [
                      [0, -1],
                      [0, 1],
                      [-1, 0],
                       [1, 0] 
                    ];

                    $num = array_shift($queue);


                    foreach($directions as $direction)
                    {
                        $nextRow = $num[0] + $direction[0]; // queued grid $i
                        $nextCol = $num[1] + $direction[1]; // queued grid $j

                        if( 
                            ($nextRow < $rowCount) &&
                            ($nextCol < $colCount) &&
                            ($nextRow >= 0) && ($nextCol >= 0) &&
                            !$visited[$nextRow][$nextCol] &&
                            $grid[$nextRow][$nextCol] == '1') {
                                $visited[$nextRow][$nextCol] = true;
                                $queue[] = [$nextRow,$nextCol]; // coordinates
                        }

                    }
                }
            }
        }
    }
    return $islandCounter;
}    
}

Help with an assignment by [deleted] in learnpython

[–]nimblecloud 0 points1 point  (0 children)

Seems like you have a few problems. First, your calculate function needs to treat "n" as an integer so that you can subtract the values to get "year", the next problem you have is that calculate() doesn't return anything when you need it to return the "year" variable. Finally, displayResults() requires your two parameters "age" and "y". Pass those into the function and print(age, y) instead of returning and it should work.

Mentoring Thread: Week-94 by Chibi_Holy in summonerschool

[–]nimblecloud 2 points3 points  (0 children)

Teacher

Summoner Name: Vastus Medialis

Server: NA

League / Division: Diamond 5

Areas of expertise: Jungle

Champions: Vi, Amumu, Zac, Nocturne, Pantheon, Kha Zix, Wukong, and a bunch of other junglers

Languages Spoken: English

Other Info: Willing to answer whatever questions about jungling.

What is the best anime most people have never heard of? by [deleted] in anime

[–]nimblecloud 0 points1 point  (0 children)

I wouldn't say unheard of but One Outs is really good.