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++
// C++ program for the above approach #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; // Keeps a track of directions // that is up, down, left, right int dx[] = { -1, 1, 0, 0 }; int dy[] = { 0, 0, -1, 1 }; // Function to perform the dfs traversal void dfs(int a, int b, int lis[][col], int& x, int& y) { // Mark the current node as visited vis[a][b] = id; // Increment length from this node length++; // Update the diameter length if (length > diameter) { x = a; y = b; diameter = length; } for (int j = 0; j < 4; j++) { // Move to next cell in x-direction int cx = a + dx[j]; // Move to next cell in y-direction int cy = b + dy[j]; // Check if cell is invalid // then continue if (cx < 0 || cy < 0 || cx >= row || cy >= col || lis[cx][cy] == 0 || vis[cx][cy]) { continue; } // Perform DFS on new cell dfs(cx, cy, lis, x, y); } vis[a][b] = 0; // Decrement the length length--; } // Function to find the maximum length of // connected 1s in the given grid void findMaximumLength(int lis[][col]) { int x, y; // Increment the id id++; length = 0; diameter = 0; // Traverse the grid[] for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (lis[i][j] != 0) { // Find start point of // start dfs call dfs(i, j, lis, x, y); i = row; break; } } } id++; length = 0; diameter = 0; // DFS Traversal from cell (x, y) dfs(x, y, lis, x, y); // Print the maximum length cout << diameter; } // Driver Code int main() { // Given grid[][] 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 } }; // Function Call findMaximumLength(grid); return 0; } |
Java
// Java program for the above approach 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; // Keeps a track of directions // that is up, down, left, right static int dx[] = { -1, 1, 0, 0 }; static int dy[] = { 0, 0, -1, 1 }; // Function to perform the dfs traversal static void dfs(int a, int b, int lis[][]) { // Mark the current node as visited vis[a][b] = id; // Increment length from this node length++; // Update the diameter length if (length > diameter) { x = a; y = b; diameter = length; } for(int j = 0; j < 4; j++) { // Move to next cell in x-direction int cx = a + dx[j]; // Move to next cell in y-direction int cy = b + dy[j]; // Check if cell is invalid // then continue if (cx < 0 || cy < 0 || cx >= row || cy >= col || lis[cx][cy] == 0 || vis[cx][cy] > 0) { continue; } // Perform DFS on new cell dfs(cx, cy, lis); } vis[a][b] = 0; // Decrement the length length--; } // Function to find the maximum length of // connected 1s in the given grid static void findMaximumLength(int lis[][]) { // Increment the id id++; length = 0; diameter = 0; // Traverse the grid[] for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { if (lis[i][j] != 0) { // Find start point of // start dfs call dfs(i, j, lis); i = row; break; } } } id++; length = 0; diameter = 0; // DFS Traversal from cell (x, y) dfs(x, y, lis); // Print the maximum length System.out.print(diameter); } // Driver Code public static void main(String[] args) { // Given grid[][] 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 } }; // Function Call findMaximumLength(grid); } } // This code is contributed by amal kumar choubey |
C#
// C# program for the above approach 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; // Keeps a track of directions // that is up, down, left, right static int []dx = { -1, 1, 0, 0 }; static int []dy = { 0, 0, -1, 1 }; // Function to perform the dfs traversal static void dfs(int a, int b, int [,]lis) { // Mark the current node as visited vis[a, b] = id; // Increment length from this node length++; // Update the diameter length if (length > diameter) { x = a; y = b; diameter = length; } for(int j = 0; j < 4; j++) { // Move to next cell in x-direction int cx = a + dx[j]; // Move to next cell in y-direction int cy = b + dy[j]; // Check if cell is invalid // then continue if (cx < 0 || cy < 0 || cx >= row || cy >= col || lis[cx, cy] == 0 || vis[cx, cy] > 0) { continue; } // Perform DFS on new cell dfs(cx, cy, lis); } vis[a, b] = 0; // Decrement the length length--; } // Function to find the maximum length of // connected 1s in the given grid static void findMaximumLength(int [,]lis) { // Increment the id id++; length = 0; diameter = 0; // Traverse the grid[] for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { if (lis[i, j] != 0) { // Find start point of // start dfs call dfs(i, j, lis); i = row; break; } } } id++; length = 0; diameter = 0; // DFS Traversal from cell (x, y) dfs(x, y, lis); // Print the maximum length Console.Write(diameter); } // Driver Code public static void Main(String[] args) { // Given grid[,] 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 } }; // Function Call findMaximumLength(grid); } } // This code is contributed by amal kumar choubey |
9
Time Complexity: O(rows + cols)
Recommended Posts:
- Convert undirected connected graph to strongly connected directed graph
- Largest connected component on a grid
- Minimum number of Water to Land conversion to make two islands connected in a Grid
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Color a grid such that all same color cells are connected either horizontally or vertically
- Check if longest connected component forms a palindrome in undirected graph
- Cycles of length n in an undirected and connected graph
- Check if the length of all connected components is a Fibonacci number
- Octal equivalents of connected components in Binary valued graph
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Length of longest subarray of length at least 2 with maximum GCD
- Length of the Longest Consecutive 1s in Binary Representation
- Length of longest consecutive ones by at most one swap in a Binary String
- Length of longest straight path from a given Binary Tree
- Length of second longest sequence of consecutive 1s in a binary array
- Strongly Connected Components
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Tarjan's Algorithm to find Strongly Connected Components
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Connected Components in an undirected graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Amal Kumar Choubey


