Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

PHP Exercises: Read the mass data and find the number of islands

PHP: Exercise-71 with Solution

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.
Write a PHP program to read the mass data and find the number of islands.

Input: A single data set is represented by 10 rows of 10 numbers representing green squares as 1 and blue squares as zeros.

Pictorial Presentation:

PHP: Read the mass data and find the number of islands.

Sample Solution: -

PHP Code:

<?php
{ 

    $field = array();
    for ($i = 0; $i < 12; $i++) {
        $field[] = str_split(rtrim(fgets(STDIN)));
    }
    fscanf(STDIN, '');
 
    $c = 0;
    for ($i = 0; $i < 10; $i++) {
        for ($j = 0; $j < 10; $j++) {
            if ($field[$i][$j] === '1') {
                dfs($field, array($i, $j));
                $c++;
            }
        }
    }
 
    echo "Number of islands: \n";
    echo $c . PHP_EOL;
}
 
function dfs(&$field, $s) {
    $stack = array($s);
    $dx = array(1, -1, 0, 0);
    $dy = array(0, 0, 1, -1);
    while (count($stack)) {
        $node = array_pop($stack);
        $field[$node[0]][$node[1]] = '0';
        for ($i = 0; $i < 4; $i++) {
            $nx = $node[1] + $dx[$i];
            $ny = $node[0] + $dy[$i];
            if (!isset($field[$ny][$nx]) || $field[$ny][$nx] === '0') {
                continue;
            }
            $stack[] = array($ny, $nx);
        }
    }
}
?>

Sample Input:
1100000111
1000000111
0000000111
0010001000
0000011100
0000111110
0001111111
1000111110
1100011100
1110001000

Sample Output:

Number of islands: 
5

Flowchart:

Flowchart: Read the mass data and find the number of islands.

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP program to find the customer number that has traded for the second consecutive for the second consecutive month from last month and the number of transactions.
Next: Write a PHP program to restore the original string by entering the compressed string with this rule.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



PHP: Tips of the Day

How to Sort Multi-dimensional Array by Value?

Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:

Example:

function sortByOrder($a, $b) {
    return $a['order'] - $b['order'];
}

usort($myArray, 'sortByOrder');

Starting in PHP 5.3, you can use an anonymous function:

usort($myArray, function($a, $b) {
    return $a['order'] - $b['order'];
});

And finally with PHP 7 you can use the spaceship operator:

usort($myArray, function($a, $b) {
    return $a['order'] <=> $b['order'];
});

To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.

usort($myArray, function($a, $b) {
    $retval = $a['order'] <=> $b['order'];
    if ($retval == 0) {
        $retval = $a['suborder'] <=> $b['suborder'];
        if ($retval == 0) {
            $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];
        }
    }
    return $retval;
});

If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual

Ref : https://bit.ly/3i77vCC