Minimize steps required to move all 1’s in a matrix to a given index
Last Updated :
30 Aug, 2022
Given a binary matrix mat[][] of size NxM and two integers X and Y, the task is to find the minimum number steps required to move all 1’s in the given matrix to the cell (X, Y), where, one step involves moving a cell left, right, up or down.
Examples:
Input: mat[][] = { {1, 0, 1}, {0, 1, 0}, {1, 0, 1} }, X = 1, Y = 1
Output: 8
Explanation:
Cells (0, 0), (0, 2), (1, 1), (2, 0) and (2, 2) consists of 1.
Moving 1 at index (0, 0) to (1, 1) requires 2 steps
(0, 0) -> (0, 1) ->(1, 0)
Moving 1 at index (0, 2) to (1, 1) requires 2 steps
Moving 1 at index (2, 0) to (1, 1) requires 2 steps
Moving 1 at index (2, 2) to (1, 1) requires 2 steps
Therefore, 8 steps are required.
Input: mat[][] = { {1, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 1, 1} }, X = 0, Y = 2
Output: 15
Approach:
The idea is to traverse the given matrix and find the cells consisting of 1. For any cell (i, j) consisting of 1, minimum steps required to reach (X, Y) based on the given directions is given by:
Minimum steps = abs(X - i) + abs(Y - j)
Calculate the total number of steps required using the above formula for each cell that contains 1 in the given matrix mat[][].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findAns(vector<vector< int > > mat,
int x, int y,
int n, int m)
{
int ans = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
if (mat[i][j] == 1) {
ans += abs (x - i)
+ abs (y - j);
}
}
}
return ans;
}
int main()
{
vector<vector< int > > mat
= { { 1, 0, 0, 0 },
{ 0, 1, 0, 1 },
{ 1, 0, 1, 1 } };
int x = 0, y = 2;
cout << findAns(mat, x, y,
mat.size(),
mat[0].size())
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findAns( int [][]mat,
int x, int y,
int n, int m)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < m; j++)
{
if (mat[i][j] == 1 )
{
ans += Math.abs(x - i) +
Math.abs(y - j);
}
}
}
return ans;
}
public static void main(String[] args)
{
int [][]mat = { { 1 , 0 , 0 , 0 },
{ 0 , 1 , 0 , 1 },
{ 1 , 0 , 1 , 1 } };
int x = 0 , y = 2 ;
System.out.print(findAns(mat, x, y,
mat.length,
mat[ 0 ].length) + "\n" );
}
}
|
Python3
def findAns(mat, x, y, n, m):
ans = 0
for i in range (n):
for j in range (m):
if (mat[i][j] = = 1 ):
ans + = ( abs (x - i) +
abs (y - j))
return ans
mat = [ [ 1 , 0 , 0 , 0 ],
[ 0 , 1 , 0 , 1 ],
[ 1 , 0 , 1 , 1 ] ]
x = 0
y = 2
print (findAns(mat, x, y, len (mat),
len (mat[ 0 ])))
|
C#
using System;
class GFG{
static int findAns( int [,]mat,
int x, int y,
int n, int m)
{
int ans = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
if (mat[i, j] == 1)
{
ans += Math.Abs(x - i) +
Math.Abs(y - j);
}
}
}
return ans;
}
public static void Main(String[] args)
{
int [,]mat = { { 1, 0, 0, 0 },
{ 0, 1, 0, 1 },
{ 1, 0, 1, 1 } };
int x = 0, y = 2;
Console.Write(findAns(mat, x, y,
mat.GetLength(0),
mat.GetLength(1)) + "\n" );
}
}
|
Javascript
<script>
function findAns(mat, x, y,
n, m)
{
let ans = 0;
for (let i = 0; i < n; i++)
{
for (let j = 0; j < m; j++)
{
if (mat[i][j] == 1)
{
ans += Math.abs(x - i) +
Math.abs(y - j);
}
}
}
return ans;
}
let mat = [[ 1, 0, 0, 0 ],
[ 0, 1, 0, 1 ],
[ 1, 0, 1, 1 ]];
let x = 0, y = 2;
document.write(findAns(mat, x, y,
mat.length,
mat[0].length) + "<br/>" );
</script>
|
Time Complexity: O(N*M), since two nested loops are used the time taken by the algorithm to complete all operations is N*M.
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...