Count of moves to escape given Matrix from given position based on given conditions
Last Updated :
03 Dec, 2021
Given an N x M matrix mat[][] where initially we are standing at the cell with index (i, j), the task is to find the number of operations required to escape the given matrix where at each operation mat[x][y] can be reached from mat[i][j] such that x represents the count of 0’s in the binary representation of mat[i][j] and y represents the count of 1’s in the binary representation of mat[i][j].
Examples:
Input: mat[][] = {{5, 13, 8, 1}, {7, 9, 2, 15}, {12, 4, 8, 3}}, i = 0, j = 0
Output: 4
Explanation: Initially, the current position is mat[0][0] = 5. Therefore, the index reachable from 5 is mat[1][2] as count of 0’s in 5 is 1 and count of 1’s in 5 is 2. Hence the first jump is from mat[0][0] => mat[1][2]. Similarly, the jumps are in the following order: mat[0][0] => mat[1][2] => mat[1][1] => mat[2][2] => mat[3][1]. Since, the index (3, 1) does not exist in the given matrix, the number of required operations are 4.
Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, i = 1, j = 2
Output: -1
Explanation: It is not possible to escape the matrix from the index.
Approach: The given problem can be solved with the help of Brian Kernighan’s Algorithm and the inbuilt log function. Below are the steps to follow:
- Create a variable Jump, which stores the required number of jump operations to escape the given 2D array. Initially, Jump = 0.
- If the initial cell is already out of bounds of the given matrix, return 0.
- Calculate the count of 0’s and 1’s in the binary representation of mat[i][j].
- Jump to the next valid index and set mat[i][j] as -1. Also, increment the value of Jump by 1.
- Check if the current value of mat[i][j] = -1 after the jump. It means that the current index is already visited, hence creating a never-ending loop. Therefore, return -1.
- Repeat the above process until the current index is already visited or it is out of bounds of the given matrix.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define Max 100
using namespace std;
int brianKernighan( int N)
{
int Count = 0;
while (N) {
N = N & (N - 1);
Count += 1;
}
return Count;
}
int getMsbIndex( int N)
{
return (( int )log2(N) + 1);
}
int countJumps( int Mat[][Max],
int N, int M,
int i, int j)
{
int C0, C1, Jump = 0;
while (i < N && j < M) {
if (Mat[i][j] == -1)
return -1;
C1 = brianKernighan(Mat[i][j]);
C0 = getMsbIndex(Mat[i][j]) - C1;
Mat[i][j] = -1;
i = C0, j = C1;
Jump++;
}
return Jump;
}
int main()
{
int N = 3, M = 4;
int i = 0, j = 0;
int Mat[][Max] = { { 5, 13, 8, 1 },
{ 7, 9, 2, 15 },
{ 12, 4, 8, 3 } };
cout << countJumps(Mat, N, M, i, j);
}
|
Java
import java.util.*;
class GFG {
static int brianKernighan( int N)
{
int Count = 0 ;
while (N != 0 ) {
N = N & (N - 1 );
Count += 1 ;
}
return Count;
}
static int getMsbIndex( int N)
{
return (( int )(Math.log(N) / Math.log( 2 )) + 1 );
}
static int countJumps( int Mat[][],
int N, int M,
int i, int j)
{
int C0, C1, Jump = 0 ;
while (i < N && j < M) {
if (Mat[i][j] == - 1 )
return - 1 ;
C1 = brianKernighan(Mat[i][j]);
C0 = getMsbIndex(Mat[i][j]) - C1;
Mat[i][j] = - 1 ;
i = C0; j = C1;
Jump++;
}
return Jump;
}
public static void main (String[] args) {
int N = 3 , M = 4 ;
int i = 0 , j = 0 ;
int Mat[][] = { { 5 , 13 , 8 , 1 },
{ 7 , 9 , 2 , 15 },
{ 12 , 4 , 8 , 3 } };
System.out.println(countJumps(Mat, N, M, i, j));
}
}
|
Python3
import math as Math
Max = 100
def brianKernighan(N):
Count = 0
while (N):
N = N & (N - 1 )
Count + = 1
return Count
def getMsbIndex(N):
return Math.floor(Math.log2(N) + 1 )
def countJumps(Mat, N, M, i, j):
Jump = 0
while (i < N and j < M):
if (Mat[i][j] = = - 1 ):
return - 1
C1 = brianKernighan(Mat[i][j])
C0 = getMsbIndex(Mat[i][j]) - C1
Mat[i][j] = - 1
i = C0
j = C1
Jump + = 1
return Jump
N = 3
M = 4
i = 0
j = 0
Mat = [[ 5 , 13 , 8 , 1 ],
[ 7 , 9 , 2 , 15 ],
[ 12 , 4 , 8 , 3 ]]
print (countJumps(Mat, N, M, i, j))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int brianKernighan( int N)
{
int Count = 0;
while (N != 0) {
N = N & (N - 1);
Count += 1;
}
return Count;
}
static int getMsbIndex( int N)
{
return (( int )(Math.Log(N) / Math.Log(2)) + 1);
}
static int countJumps( int [,] Mat,
int N, int M,
int i, int j)
{
int C0, C1, Jump = 0;
while (i < N && j < M) {
if (Mat[i, j] == -1)
return -1;
C1 = brianKernighan(Mat[i, j]);
C0 = getMsbIndex(Mat[i, j]) - C1;
Mat[i, j] = -1;
i = C0; j = C1;
Jump++;
}
return Jump;
}
public static void Main()
{
int N = 3, M = 4;
int i = 0, j = 0;
int [,] Mat = {{ 5, 13, 8, 1 },
{ 7, 9, 2, 15 },
{ 12, 4, 8, 3 } };
Console.Write(countJumps(Mat, N, M, i, j));
}
}
|
Javascript
<script>
var Max = 100
function brianKernighan(N) {
let Count = 0;
while (N) {
N = N & (N - 1);
Count += 1;
}
return Count;
}
function getMsbIndex(N) {
return Math.floor(Math.log2(N) + 1);
}
function countJumps(Mat,
N, M,
i, j) {
let C0, C1, Jump = 0;
while (i < N && j < M) {
if (Mat[i][j] == -1)
return -1;
C1 = brianKernighan(Mat[i][j]);
C0 = getMsbIndex(Mat[i][j]) - C1;
Mat[i][j] = -1;
i = C0, j = C1;
Jump++;
}
return Jump;
}
let N = 3, M = 4;
let i = 0, j = 0;
let Mat = [[5, 13, 8, 1],
[7, 9, 2, 15],
[12, 4, 8, 3]];
document.write(countJumps(Mat, N, M, i, j));
</script>
|
Time Complexity: O(N * M * log(M * N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...