Find minimum moves to bring all elements in one cell of a matrix
Given a matrix mat[][], pair of indices X and Y, the task is to find the number of moves to bring all the non-zero elements of the matrix to the given cell at (X, Y).
A move consists of moving an element at any cell to its four directional adjacent cells i.e., left, right, top, bottom.
Examples:
Input: mat[][] = {{1, 0}, {1, 0}}, X = 1, Y = 1
Output: 3
Explanation:
Moves required =>
For Index (0, 0) => 2
For Index (1, 0) => 1
Total moves required = 3
Input: mat[][] = {{1, 0, 1, 0}, {1, 1, 0, 1}, {0, 0, 1, 0}}, X = 1, Y = 3
Output: 13
Approach: The idea is to traverse the matrix and for each non-zero element of the matrix find the distance of the current cell(say (A, B)) to the destination cell (X, Y) of the matrix as:
moves = abs(x - i) + abs(y - j)
The summation of all the distances by the above formula for all non-zero elements is the required result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int M = 4;
const int N = 5;
void no_of_moves( int Matrix[M][N],
int x, int y)
{
int moves = 0;
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
if (Matrix[i][j] != 0) {
moves += abs (x - i);
moves += abs (y - j);
}
}
}
cout << moves << "\n" ;
}
int main()
{
int x = 3;
int y = 2;
int Matrix[M][N] = { { 1, 0, 1, 1, 0 },
{ 0, 1, 1, 0, 1 },
{ 0, 0, 1, 1, 0 },
{ 1, 1, 1, 0, 0 } };
int num = 1;
no_of_moves(Matrix, x, y);
return 0;
}
|
Java
class GFG{
static int M = 4 ;
static int N = 5 ;
public static void no_of_moves( int [][] Matrix,
int x, int y)
{
int moves = 0 ;
for ( int i = 0 ; i < M; i++)
{
for ( int j = 0 ; j < N; j++)
{
if (Matrix[i][j] != 0 )
{
moves += Math.abs(x - i);
moves += Math.abs(y - j);
}
}
}
System.out.println(moves);
}
public static void main(String[] args)
{
int x = 3 ;
int y = 2 ;
int [][] Matrix = { { 1 , 0 , 1 , 1 , 0 },
{ 0 , 1 , 1 , 0 , 1 },
{ 0 , 0 , 1 , 1 , 0 },
{ 1 , 1 , 1 , 0 , 0 } };
int num = 1 ;
no_of_moves(Matrix, x, y);
}
}
|
Python3
M = 4
N = 5
def no_of_moves(Matrix, x, y):
moves = 0
for i in range (M):
for j in range (N):
if (Matrix[i][j] ! = 0 ):
moves + = abs (x - i)
moves + = abs (y - j)
print (moves)
if __name__ = = '__main__' :
x = 3
y = 2
Matrix = [ [ 1 , 0 , 1 , 1 , 0 ],
[ 0 , 1 , 1 , 0 , 1 ],
[ 0 , 0 , 1 , 1 , 0 ],
[ 1 , 1 , 1 , 0 , 0 ] ]
num = 1
no_of_moves(Matrix, x, y)
|
C#
using System;
class GFG{
static int M = 4;
static int N = 5;
public static void no_of_moves( int [,] Matrix,
int x, int y)
{
int moves = 0;
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
if (Matrix[i, j] != 0)
{
moves += Math.Abs(x - i);
moves += Math.Abs(y - j);
}
}
}
Console.WriteLine(moves);
}
public static void Main(String[] args)
{
int x = 3;
int y = 2;
int [,] Matrix = { { 1, 0, 1, 1, 0 },
{ 0, 1, 1, 0, 1 },
{ 0, 0, 1, 1, 0 },
{ 1, 1, 1, 0, 0 } };
no_of_moves(Matrix, x, y);
}
}
|
Javascript
<script>
let M = 4;
let N = 5;
function no_of_moves(Matrix, x, y)
{
let moves = 0;
for (let i = 0; i < M; i++)
{
for (let j = 0; j < N; j++)
{
if (Matrix[i][j] != 0)
{
moves += Math.abs(x - i);
moves += Math.abs(y - j);
}
}
}
document.write(moves);
}
let x = 3;
let y = 2;
let Matrix = [[ 1, 0, 1, 1, 0 ],
[ 0, 1, 1, 0, 1 ],
[ 0, 0, 1, 1, 0 ],
[ 1, 1, 1, 0, 0 ]];
let num = 1;
no_of_moves(Matrix, x, y);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
11 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...