Count number of islands where every island is row-wise and column-wise separated
Last Updated :
14 Sep, 2023
Given a rectangular matrix which has only two possible values ‘X’ and ‘O’. The values ‘X’ always appear in form of rectangular islands and these islands are always row-wise and column-wise separated by at least one line of ‘O’s. Note that islands can only be diagonally adjacent. Count the number of islands in the given matrix.
Examples:
mat[M][N] = {{'O', 'O', 'O'},
{'X', 'X', 'O'},
{'X', 'X', 'O'},
{'O', 'O', 'X'},
{'O', 'O', 'X'},
{'X', 'X', 'O'}
};
Output: Number of islands is 3
mat[M][N] = {{'X', 'O', 'O', 'O', 'O', 'O'},
{'X', 'O', 'X', 'X', 'X', 'X'},
{'O', 'O', 'O', 'O', 'O', 'O'},
{'X', 'X', 'X', 'O', 'X', 'X'},
{'X', 'X', 'X', 'O', 'X', 'X'},
{'O', 'O', 'O', 'O', 'X', 'X'},
};
Output: Number of islands is 4
We strongly recommend to minimize your browser and try this yourself first.
The idea is to count all top-leftmost corners of given matrix. We can check if a ‘X’ is top left or not by checking following conditions.
- A ‘X’ is top of rectangle if the cell just above it is a ‘O’
- A ‘X’ is leftmost of rectangle if the cell just left of it is a ‘O’
Note that we must check for both conditions as there may be more than one top cells and more than one leftmost cells in a rectangular island. Below is the implementation of above idea.
C++
#include<iostream>
using namespace std;
#define M 6
#define N 3
int countIslands( int mat[][N])
{
int count = 0;
for ( int i=0; i<M; i++)
{
for ( int j=0; j<N; j++)
{
if (mat[i][j] == 'X' )
{
if ((i == 0 || mat[i-1][j] == 'O' ) &&
(j == 0 || mat[i][j-1] == 'O' ))
count++;
}
}
}
return count;
}
int main()
{
int mat[M][N] = {{ 'O' , 'O' , 'O' },
{ 'X' , 'X' , 'O' },
{ 'X' , 'X' , 'O' },
{ 'O' , 'O' , 'X' },
{ 'O' , 'O' , 'X' },
{ 'X' , 'X' , 'O' }
};
cout << "Number of rectangular islands is "
<< countIslands(mat);
return 0;
}
|
Java
import java.io.*;
class islands
{
static int countIslands( int mat[][], int m, int n)
{
int count = 0 ;
for ( int i= 0 ; i<m; i++)
{
for ( int j= 0 ; j<n; j++)
{
if (mat[i][j] == 'X' )
{
if ((i == 0 || mat[i- 1 ][j] == 'O' ) &&
(j == 0 || mat[i][j- 1 ] == 'O' ))
count++;
}
}
}
return count;
}
public static void main (String[] args)
{
int m = 6 ;
int n = 3 ;
int mat[][] = {{ 'O' , 'O' , 'O' },
{ 'X' , 'X' , 'O' },
{ 'X' , 'X' , 'O' },
{ 'O' , 'O' , 'X' },
{ 'O' , 'O' , 'X' },
{ 'X' , 'X' , 'O' }
};
System.out.println( "Number of rectangular islands is: "
+ countIslands(mat, m, n));
}
}
|
Python3
M = 6
N = 3
def countIslands(mat):
count = 0 ;
for i in range ( 0 , M):
for j in range ( 0 , N):
if (mat[i][j] = = 'X' ):
if ((i = = 0 or mat[i - 1 ][j] = = 'O' ) and
(j = = 0 or mat[i][j - 1 ] = = 'O' )):
count = count + 1
return count
mat = [[ 'O' , 'O' , 'O' ],
[ 'X' , 'X' , 'O' ],
[ 'X' , 'X' , 'O' ],
[ 'O' , 'O' , 'X' ],
[ 'O' , 'O' , 'X' ],
[ 'X' , 'X' , 'O' ]]
print ( "Number of rectangular islands is" ,
countIslands(mat))
|
C#
using System;
class GFG {
static int countIslands( int [,]mat, int m, int n)
{
int count = 0;
for ( int i = 0; i < m; i++)
{
for ( int j = 0; j < n; j++)
{
if (mat[i,j] == 'X' )
{
if ((i == 0 || mat[i-1,j] == 'O' ) &&
(j == 0 || mat[i,j-1] == 'O' ))
count++;
}
}
}
return count;
}
public static void Main ()
{
int m = 6;
int n = 3;
int [,]mat = { { 'O' , 'O' , 'O' },
{ 'X' , 'X' , 'O' },
{ 'X' , 'X' , 'O' },
{ 'O' , 'O' , 'X' },
{ 'O' , 'O' , 'X' },
{ 'X' , 'X' , 'O' }
};
Console.WriteLine( "Number of rectangular "
+ "islands is: " + countIslands(mat, m, n));
}
}
|
Javascript
<script>
function countIslands(mat, m, n)
{
let count = 0;
for (let i = 0; i < m; i++)
{
for (let j = 0; j < n; j++)
{
if (mat[i][j] == 'X' )
{
if ((i == 0 || mat[i-1][j] == 'O' ) &&
(j == 0 || mat[i][j-1] == 'O' ))
count++;
}
}
}
return count;
}
let m = 6;
let n = 3;
let mat = [[ 'O' , 'O' , 'O' ],
[ 'X' , 'X' , 'O' ],
[ 'X' , 'X' , 'O' ],
[ 'O' , 'O' , 'X' ],
[ 'O' , 'O' , 'X' ],
[ 'X' , 'X' , 'O' ]
];
document.write( "Number of rectangular islands is "
+ countIslands(mat, m, n));
</script>
|
PHP
<?php
function countIslands( $mat )
{
$M = 6;
$N = 3;
$count = 0;
for ( $i = 0; $i < $M ; $i ++)
{
for ( $j = 0; $j < $N ; $j ++)
{
if ( $mat [ $i ][ $j ] == 'X' )
{
if (( $i == 0 || $mat [ $i - 1][ $j ] == 'O' ) &&
( $j == 0 || $mat [ $i ][ $j -1] == 'O' ))
$count ++;
}
}
}
return $count ;
}
$mat = array ( array ( 'O' , 'O' , 'O' ),
array ( 'X' , 'X' , 'O' ),
array ( 'X' , 'X' , 'O' ),
array ( 'O' , 'O' , 'X' ),
array ( 'O' , 'O' , 'X' ),
array ( 'X' , 'X' , 'O' ));
echo "Number of rectangular islands is "
, countIslands( $mat );
?>
|
Output
Number of rectangular islands is 3
Time complexity of this solution is O(M x N).
Auxiliary Space: O(1)
Approach 2:
To implement this using a dynamic programming (DP) approach, we need to modify the logic of counting islands. Instead of checking each cell individually, we’ll maintain a DP table to store the count of islands up to a particular cell.
Here’s the modified version of the program using a DP approach:
C++
#include<iostream>
using namespace std;
#define M 6
#define N 3
int countIslands( int mat[][N])
{
int dp[M][N];
int count = 0;
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
if (mat[i][j] == 'O' )
dp[i][j] = 0;
else
{
if (j == 0 || mat[i][j - 1] == 'O' )
dp[i][j] = 1;
else
dp[i][j] = dp[i][j - 1] + 1;
count += dp[i][j];
}
}
}
return count;
}
int main()
{
int mat[M][N] = {{ 'O' , 'O' , 'O' },
{ 'X' , 'X' , 'O' },
{ 'X' , 'X' , 'O' },
{ 'O' , 'O' , 'X' },
{ 'O' , 'O' , 'X' },
{ 'X' , 'X' , 'O' }
};
cout << "Number of rectangular islands is "
<< countIslands(mat);
return 0;
}
|
Java
import java.util.*;
public class Main {
static final int M = 6 ;
static final int N = 3 ;
static int countIslands( char [][] mat) {
int [][] dp = new int [M][N];
int count = 0 ;
for ( int i = 0 ; i < M; i++) {
for ( int j = 0 ; j < N; j++) {
if (mat[i][j] == 'O' )
dp[i][j] = 0 ;
else {
if (j == 0 || mat[i][j - 1 ] == 'O' )
dp[i][j] = 1 ;
else
dp[i][j] = dp[i][j - 1 ] + 1 ;
count += dp[i][j];
}
}
}
return count;
}
public static void main(String[] args) {
char [][] mat = {
{ 'O' , 'O' , 'O' },
{ 'X' , 'X' , 'O' },
{ 'X' , 'X' , 'O' },
{ 'O' , 'O' , 'X' },
{ 'O' , 'O' , 'X' },
{ 'X' , 'X' , 'O' }
};
System.out.println( "Number of rectangular islands is " + countIslands(mat));
}
}
|
Python3
M = 6
N = 3
def countIslands(mat):
dp = [[ 0 ] * N for _ in range (M)]
count = 0
for i in range (M):
for j in range (N):
if mat[i][j] = = 'O' :
dp[i][j] = 0
else :
if j = = 0 or mat[i][j - 1 ] = = 'O' :
dp[i][j] = 1
else :
dp[i][j] = dp[i][j - 1 ] + 1
count + = dp[i][j]
return count
if __name__ = = "__main__" :
mat = [[ 'O' , 'O' , 'O' ],
[ 'X' , 'X' , 'O' ],
[ 'X' , 'X' , 'O' ],
[ 'O' , 'O' , 'X' ],
[ 'O' , 'O' , 'X' ],
[ 'X' , 'X' , 'O' ]]
print ( "Number of rectangular islands is" , countIslands(mat))
|
C#
using System;
public class GFG
{
const int M = 6;
const int N = 3;
static int CountIslands( char [][] mat)
{
int [][] dp = new int [M][];
for ( int i = 0; i < M; i++)
{
dp[i] = new int [N];
}
int count = 0;
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
if (mat[i][j] == 'O' )
dp[i][j] = 0;
else
{
if (j == 0 || mat[i][j - 1] == 'O' )
dp[i][j] = 1;
else
dp[i][j] = dp[i][j - 1] + 1;
count += dp[i][j];
}
}
}
return count;
}
static void Main()
{
char [][] mat = new char [][] {
new char [] { 'O' , 'O' , 'O' },
new char [] { 'X' , 'X' , 'O' },
new char [] { 'X' , 'X' , 'O' },
new char [] { 'O' , 'O' , 'X' },
new char [] { 'O' , 'O' , 'X' },
new char [] { 'X' , 'X' , 'O' }
};
Console.WriteLine( "Number of rectangular islands is " + CountIslands(mat));
}
}
|
Javascript
const M = 6;
const N = 3;
function countIslands(mat) {
const dp = new Array(M).fill(0).map(() => new Array(N).fill(0));
let count = 0;
for (let i = 0; i < M; i++) {
for (let j = 0; j < N; j++) {
if (mat[i][j] === 'O' )
dp[i][j] = 0;
else {
if (j === 0 || mat[i][j - 1] === 'O' )
dp[i][j] = 1;
else
dp[i][j] = dp[i][j - 1] + 1;
count += dp[i][j];
}
}
}
return count;
}
const mat = [
[ 'O' , 'O' , 'O' ],
[ 'X' , 'X' , 'O' ],
[ 'X' , 'X' , 'O' ],
[ 'O' , 'O' , 'X' ],
[ 'O' , 'O' , 'X' ],
[ 'X' , 'X' , 'O' ]
];
console.log( "Number of rectangular islands is " + countIslands(mat));
|
Output
Number of rectangular islands is 11
Time complexity of this solution is O(M x N).
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...