there are 10 vertical and horizontal squares on a plane. each square is painted blue and green. blue represents the sea, and green represents the land. when two green squares are in contact with the top and bottom, or right and left, they are said to be ground. the area created by only one green square is called "island". for example, there are five islands in the figure below in PHP

PHP

🤖 Code Explanation

This is a function that counts the number of islands in a two dimensional array. The function uses a depth first search algorithm to explore all possible directions from the current cell. If the current cell is land, it is marked as visited and the function explores all possible directions from that cell. If the current cell is water, the function backtracks from that cell.

More problems solved in PHP

Download SpellBox today