Min number of moves to traverse entire Matrix through connected cells with equal values
Given a matrix A[][] of dimension N*M, the task is to find the minimum number of moves required to traverse the entire matrix starting from (0, 0) by traversing connected cells with equal values at every step.
From a cell (i, j), cells (i + 1, j), (i – 1, j), (i, j – 1) and (i, j + 1) can be connected.
Examples:
Input: arr[][] = {{1, 1, 2, 2, 1}, {1, 1, 2, 2, 1}, {1, 1, 1, 3, 2}}
Output: 5
Explanation:
Minimum 5 moves are required to traverse the matrix.
First move: Starting from [0, 0], traverse cells [0, 1], [1, 1], [1, 0], [2, 0], [2, 1], [2, 2] as all these cells have the same value, 1.
Second move: Traverse cells [0, 2], [0, 3], [1, 3], [1, 2] as all these cells have value 2.
Third move: Traverse cells [0, 4], [1, 4] containing 1.
Fourth move: Traverse [2, 3] containing 3.
Fifth move: Traverse [2, 4] containing 4.
Input: arr[][] = {{2, 1, 3}, {1, 1, 2}}
Output: 4
Explanation:
Minimum 4 moves are required to cover this 2-D array
First move: Traverse only [0, 0] as no other connected cell has value 2.
Second move: Traverse cells [0, 1], [1, 1], [1, 0] as these cells contain value 1.
Third move: Traverse cell [0, 2] containing 3.
Fourth move: Traverse cell [1, 2] containing 2.
Approach:
Follow the steps below to solve the problem:
- Create another matrix to fill each cell with distinct values.
- Traverse matrix A[][] starting from (0, 0). For every cell (i, j), check if its adjacent cells have the same value as A[i][j] or not.
- If any adjacent cell has the same value, replace that cell in B[][] with the value of B[i][j].
- The counting of remaining distinct elements in the B[][] matrix after completing the traversal of A[][], gives the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int A[][10], int N, int M)
{
int B[N][M];
int c = 1;
set< int > s;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++)
B[i][j] = c++;
}
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
if (i != 0) {
if (A[i - 1][j] == A[i][j])
B[i - 1][j] = B[i][j];
}
if (i != N - 1) {
if (A[i + 1][j] == A[i][j])
B[i + 1][j] = B[i][j];
}
if (j != 0) {
if (A[i][j - 1] == A[i][j])
B[i][j - 1] = B[i][j];
}
if (j != M - 1) {
if (A[i][j + 1] == A[i][j])
B[i][j + 1] = B[i][j];
}
}
}
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++)
s.insert(B[i][j]);
}
return s.size();
}
int main()
{
int N = 2, M = 3;
int A[][10] = { { 2, 1, 3 },
{ 1, 1, 2 } };
cout << solve(A, N, M);
}
|
Java
import java.util.*;
class GFG{
static int solve( int A[][], int N,
int M)
{
int [][]B = new int [N][M];
int c = 1 ;
HashSet<Integer> s = new HashSet<Integer>();
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < M; j++)
B[i][j] = c++;
}
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < M; j++)
{
if (i != 0 )
{
if (A[i - 1 ][j] == A[i][j])
B[i - 1 ][j] = B[i][j];
}
if (i != N - 1 )
{
if (A[i + 1 ][j] == A[i][j])
B[i + 1 ][j] = B[i][j];
}
if (j != 0 )
{
if (A[i][j - 1 ] == A[i][j])
B[i][j - 1 ] = B[i][j];
}
if (j != M - 1 )
{
if (A[i][j + 1 ] == A[i][j])
B[i][j + 1 ] = B[i][j];
}
}
}
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < M; j++)
s.add(B[i][j]);
}
return s.size();
}
public static void main(String[] args)
{
int N = 2 , M = 3 ;
int A[][] = { { 2 , 1 , 3 },
{ 1 , 1 , 2 } };
System.out.print(solve(A, N, M));
}
}
|
Python3
def solve(A, N, M):
B = []
c = 1
s = set ()
for i in range (N):
new = []
for j in range (M):
new.append(c)
c = c + 1
B.append(new)
for i in range (N):
for j in range (M):
if i ! = 0 :
if A[i - 1 ][j] = = A[i][j]:
B[i - 1 ][j] = B[i][j]
if (i ! = N - 1 ):
if A[i + 1 ][j] = = A[i][j]:
B[i + 1 ][j] = B[i][j]
if (j ! = 0 ):
if A[i][j - 1 ] = = A[i][j]:
B[i][j - 1 ] = B[i][j]
if (j ! = M - 1 ):
if (A[i][j + 1 ] = = A[i][j]):
B[i][j + 1 ] = B[i][j]
for i in range (N):
for j in range (M):
s.add(B[i][j])
return len (s)
N = 2
M = 3
A = [ [ 2 , 1 , 3 ], [ 1 , 1 , 2 ] ]
print (solve(A, N, M))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int solve( int [,]A, int N,
int M)
{
int [,]B = new int [N, M];
int c = 1;
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
B[i, j] = c++;
}
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
{
if (i != 0)
{
if (A[i - 1, j] == A[i, j])
B[i - 1, j] = B[i, j];
}
if (i != N - 1)
{
if (A[i + 1, j] == A[i, j])
B[i + 1, j] = B[i, j];
}
if (j != 0)
{
if (A[i, j - 1] == A[i, j])
B[i, j - 1] = B[i, j];
}
if (j != M - 1)
{
if (A[i, j + 1] == A[i, j])
B[i, j + 1] = B[i, j];
}
}
}
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
s.Add(B[i, j]);
}
return s.Count;
}
public static void Main(String[] args)
{
int N = 2, M = 3;
int [,]A = { { 2, 1, 3 },
{ 1, 1, 2 } };
Console.Write(solve(A, N, M));
}
}
|
Javascript
<script>
function solve(A, N, M)
{
var B = Array.from(Array(N), ()=> Array(M));
var c = 1;
var s = new Set();
for ( var i = 0; i < N; i++) {
for ( var j = 0; j < M; j++)
B[i][j] = c++;
}
for ( var i = 0; i < N; i++) {
for ( var j = 0; j < M; j++) {
if (i != 0) {
if (A[i - 1][j] == A[i][j])
B[i - 1][j] = B[i][j];
}
if (i != N - 1) {
if (A[i + 1][j] == A[i][j])
B[i + 1][j] = B[i][j];
}
if (j != 0) {
if (A[i][j - 1] == A[i][j])
B[i][j - 1] = B[i][j];
}
if (j != M - 1) {
if (A[i][j + 1] == A[i][j])
B[i][j + 1] = B[i][j];
}
}
}
for ( var i = 0; i < N; i++) {
for ( var j = 0; j < M; j++)
s.add(B[i][j]);
}
return s.size;
}
var N = 2, M = 3;
var A = [ [ 2, 1, 3 ],
[ 1, 1, 2 ]];
document.write( solve(A, N, M));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Last Updated :
27 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...