Find if a 2-D array is completely traversed or not by following the cell values
Last Updated :
18 Jul, 2022
You are given a 2-D array. We have to traverse each and every cell of the given array by following the cell locations then return true else false. The value of each cell is given by (x, y) where (x, y) is also shown next following cell position. Eg. (0, 0) shows starting cell. And ‘null’ shows our final destination after traversing every cell.
Examples:
Input : { 0, 1 1, 2 1, 1
0, 2 2, 0 2, 1
0, 0 1, 0 null }
Output : false
Input : { 0, 1 2, 0
null 1, 0
2, 1 1, 1 }
Output : true
We take a visited array if we visit a cell then make its value true in the visited array so that we can capture the cycle in our grid for next time when we visit it again. And if we find null before completing the loop then it means we didn’t traversed all the cell of given array.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isAllCellTraversed(vector<vector<pair< int , int >>>grid, int n, int m)
{
bool visited[n][m];
int total = n*m;
int startx = grid[0][0].first;
int starty = grid[0][0].second;
for ( int i = 0; i < total - 2; i++)
{
if (grid[startx][starty].first == -1 and
grid[startx][starty].second == -1)
return false ;
if (visited[startx][starty] == true )
return false ;
visited[startx][starty] = true ;
int x = grid[startx][starty].first;
int y = grid[startx][starty].second;
startx = x;
starty = y;
}
if (grid[startx][starty].first == -1 and
grid[startx][starty].second == -1)
return true ;
return false ;
}
int main()
{
vector<vector<pair< int , int >>> cell(3, vector<pair< int , int >> (2));
cell[0][0] = {0, 1};
cell[0][1] = {2, 0};
cell[1][0] = {-1,-1};
cell[1][1] = {1, 0};
cell[2][0] = {2, 1};
cell[2][1] = {1, 1};
if (!isAllCellTraversed(cell, 3, 2))
cout << "true" ;
else
cout << "false" ;
return 0;
}
|
Java
import java.io.*;
class Cell {
int x;
int y;
Cell( int x, int y)
{
this .x = x;
this .y = y;
}
}
public class MoveCellPerCellValue {
public boolean isAllCellTraversed(Cell grid[][])
{
boolean [][] visited =
new boolean [grid.length][grid[ 0 ].length];
int total = grid.length * grid[ 0 ].length;
int startx = grid[ 0 ][ 0 ].x;
int starty = grid[ 0 ][ 0 ].y;
for ( int i = 0 ; i < total - 2 ; i++) {
if (grid[startx][starty] == null )
return false ;
if (visited[startx][starty] == true )
return false ;
visited[startx][starty] = true ;
int x = grid[startx][starty].x;
int y = grid[startx][starty].y;
startx = x;
starty = y;
}
if (grid[startx][starty] == null )
return true ;
return false ;
}
public static void main(String args[])
{
Cell cell[][] = new Cell[ 3 ][ 2 ];
cell[ 0 ][ 0 ] = new Cell( 0 , 1 );
cell[ 0 ][ 1 ] = new Cell( 2 , 0 );
cell[ 1 ][ 0 ] = null ;
cell[ 1 ][ 1 ] = new Cell( 1 , 0 );
cell[ 2 ][ 0 ] = new Cell( 2 , 1 );
cell[ 2 ][ 1 ] = new Cell( 1 , 1 );
MoveCellPerCellValue mcp = new MoveCellPerCellValue();
System.out.println(mcp.isAllCellTraversed(cell));
}
}
|
Python3
C#
Javascript
Time Complexity : O(N)
Auxiliary Space : O(M*N)
Share your thoughts in the comments
Please Login to comment...