Length of longest connected 1’s in a Binary Grid
Given a grid of size N*M consists of 0 and 1 only, the task is to find the length of longest connected 1s in the given grid. We can only move to left, right, up or down from any current cell of the grid.
Examples:
Input: N = 3, M = 3, grid[][] = { {0, 0, 0}, {0, 1, 0}, {0, 0, 0} }
Output: 1
Explanation:
The longest possible route is 1 as there cant be any movement from (1, 1) position of the matrix.
Input: N = 6, M = 7, grid[][] = { {0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 0, 0, 0}, {0, 1, 0, 1, 0, 0, 0}, {0, 1, 0, 1, 0, 1, 0}, {0, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 0, 0, 0}}
Output: 9
Explanation:
The longest possible route is 9 starting from (1, 1) -> (2, 1) -> (3, 1) -> (4, 1) -> (4, 2) -> (4, 3) -> (4, 4) -> (4, 5) -> (3, 5).
Approach: The idea is to do DFS Traversal on grid where the value of current cell is 1 and recursively call for all the four direction of the current cell where value is 1 and updated the maximum length of connected 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define row 6
#define col 7
using namespace std;
int vis[row + 1][col + 1], id;
int diameter = 0, length = 0;
int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };
void dfs( int a, int b, int lis[][col],
int & x, int & y)
{
vis[a][b] = id;
length++;
if (length > diameter) {
x = a;
y = b;
diameter = length;
}
for ( int j = 0; j < 4; j++) {
int cx = a + dx[j];
int cy = b + dy[j];
if (cx < 0 || cy < 0 || cx >= row
|| cy >= col || lis[cx][cy] == 0
|| vis[cx][cy]) {
continue ;
}
dfs(cx, cy, lis, x, y);
}
vis[a][b] = 0;
length--;
}
void findMaximumLength( int lis[][col])
{
int x, y;
id++;
length = 0;
diameter = 0;
for ( int i = 0; i < row; i++) {
for ( int j = 0; j < col; j++) {
if (lis[i][j] != 0) {
dfs(i, j, lis, x, y);
i = row;
break ;
}
}
}
id++;
length = 0;
diameter = 0;
dfs(x, y, lis, x, y);
cout << diameter;
}
int main()
{
int grid[][col] = { { 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 0 },
{ 0, 0, 0, 1, 0, 0, 0 } };
findMaximumLength(grid);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int row = 6 ;
static final int col = 7 ;
static int [][]vis = new int [row + 1 ][col + 1 ];
static int id;
static int diameter = 0 , length = 0 ;
static int x = 0 , y = 0 ;
static int dx[] = { - 1 , 1 , 0 , 0 };
static int dy[] = { 0 , 0 , - 1 , 1 };
static void dfs( int a, int b, int lis[][])
{
vis[a][b] = id;
length++;
if (length > diameter)
{
x = a;
y = b;
diameter = length;
}
for ( int j = 0 ; j < 4 ; j++)
{
int cx = a + dx[j];
int cy = b + dy[j];
if (cx < 0 || cy < 0 ||
cx >= row || cy >= col ||
lis[cx][cy] == 0 || vis[cx][cy] > 0 )
{
continue ;
}
dfs(cx, cy, lis);
}
vis[a][b] = 0 ;
length--;
}
static void findMaximumLength( int lis[][])
{
id++;
length = 0 ;
diameter = 0 ;
for ( int i = 0 ; i < row; i++)
{
for ( int j = 0 ; j < col; j++)
{
if (lis[i][j] != 0 )
{
dfs(i, j, lis);
i = row;
break ;
}
}
}
id++;
length = 0 ;
diameter = 0 ;
dfs(x, y, lis);
System.out.print(diameter);
}
public static void main(String[] args)
{
int grid[][] = { { 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{ 0 , 1 , 0 , 1 , 0 , 0 , 0 },
{ 0 , 1 , 0 , 1 , 0 , 0 , 0 },
{ 0 , 1 , 0 , 1 , 0 , 1 , 0 },
{ 0 , 1 , 1 , 1 , 1 , 1 , 0 },
{ 0 , 0 , 0 , 1 , 0 , 0 , 0 } };
findMaximumLength(grid);
}
}
|
Python3
row = 6
col = 7
vis = [[ 0 for i in range (col + 1 )]
for j in range (row + 1 )]
id = 0
diameter = 0
length = 0
dx = [ - 1 , 1 , 0 , 0 ]
dy = [ 0 , 0 , - 1 , 1 ]
def dfs(a, b, lis, x, y):
global id , length, diameter
vis[a][b] = id
length + = 1
if (length > diameter):
x = a
y = b
diameter = length
for j in range ( 4 ):
cx = a + dx[j]
cy = b + dy[j]
if (cx < 0 or cy < 0 or
cx > = row or cy > = col or
lis[cx][cy] = = 0 or vis[cx][cy]):
continue
dfs(cx, cy, lis, x, y)
vis[a][b] = 0
length - = 1
return x, y
def findMaximumLength(lis):
global id , length, diameter
x = 0
y = 0
id + = 1
length = 0
diameter = 0
for i in range (row):
for j in range (col):
if (lis[i][j] ! = 0 ):
x, y = dfs(i, j, lis, x, y)
i = row
break
id + = 1
length = 0
diameter = 0
x, y = dfs(x, y, lis, x, y)
print (diameter)
if __name__ = = "__main__" :
grid = [ [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ],
[ 0 , 1 , 0 , 1 , 0 , 0 , 0 ],
[ 0 , 1 , 0 , 1 , 0 , 0 , 0 ],
[ 0 , 1 , 0 , 1 , 0 , 1 , 0 ],
[ 0 , 1 , 1 , 1 , 1 , 1 , 0 ],
[ 0 , 0 , 0 , 1 , 0 , 0 , 0 ] ]
findMaximumLength(grid)
|
C#
using System;
class GFG{
static readonly int row = 6;
static readonly int col = 7;
static int [,]vis = new int [row + 1, col + 1];
static int id;
static int diameter = 0, length = 0;
static int x = 0, y = 0;
static int []dx = { -1, 1, 0, 0 };
static int []dy = { 0, 0, -1, 1 };
static void dfs( int a, int b, int [,]lis)
{
vis[a, b] = id;
length++;
if (length > diameter)
{
x = a;
y = b;
diameter = length;
}
for ( int j = 0; j < 4; j++)
{
int cx = a + dx[j];
int cy = b + dy[j];
if (cx < 0 || cy < 0 ||
cx >= row || cy >= col ||
lis[cx, cy] == 0 || vis[cx, cy] > 0)
{
continue ;
}
dfs(cx, cy, lis);
}
vis[a, b] = 0;
length--;
}
static void findMaximumLength( int [,]lis)
{
id++;
length = 0;
diameter = 0;
for ( int i = 0; i < row; i++)
{
for ( int j = 0; j < col; j++)
{
if (lis[i, j] != 0)
{
dfs(i, j, lis);
i = row;
break ;
}
}
}
id++;
length = 0;
diameter = 0;
dfs(x, y, lis);
Console.Write(diameter);
}
public static void Main(String[] args)
{
int [,]grid = { { 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 0 },
{ 0, 0, 0, 1, 0, 0, 0 } };
findMaximumLength(grid);
}
}
|
Javascript
<script>
let row = 6;
let col = 7;
let vis = new Array(row + 1);
for ( var i = 0; i < vis.length; i++) {
vis[i] = new Array(2);
}
let id = 0;
let diameter = 0, length = 0;
let x = 0, y = 0;
let dx = [ -1, 1, 0, 0 ];
let dy = [ 0, 0, -1, 1 ];
function dfs(a, b, lis)
{
vis[a][b] = id;
length++;
if (length > diameter)
{
x = a;
y = b;
diameter = length;
}
for (let j = 0; j < 4; j++)
{
let cx = a + dx[j];
let cy = b + dy[j];
if (cx < 0 || cy < 0 ||
cx >= row || cy >= col ||
lis[cx][cy] == 0 || vis[cx][cy] > 0)
{
continue ;
}
dfs(cx, cy, lis);
}
vis[a][b] = 0;
length--;
}
function findMaximumLength(lis)
{
id++;
length = 0;
diameter = 0;
for (let i = 0; i < row; i++)
{
for (let j = 0; j < col; j++)
{
if (lis[i][j] != 0)
{
dfs(i, j, lis);
i = row;
break ;
}
}
}
id++;
length = 0;
diameter = 0;
dfs(x, y, lis);
document.write(diameter);
}
let grid = [[ 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1, 0, 1, 0, 0, 0 ],
[ 0, 1, 0, 1, 0, 0, 0 ],
[ 0, 1, 0, 1, 0, 1, 0 ],
[ 0, 1, 1, 1, 1, 1, 0 ],
[ 0, 0, 0, 1, 0, 0, 0 ]];
findMaximumLength(grid);
</script>
|
Time Complexity: O(N*M)
where ‘N’ is the number of rows and ‘M’ is number of columns.
Last Updated :
12 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...