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++
// C++ program to calculate // the minimum steps // required to reach // a given cell from all // cells consisting of 1's #include <bits/stdc++.h> using namespace std; // Function to calculate and // return the minimum // number of steps required // to move all 1s to (X, Y) int findAns(vector<vector<int> > mat, int x, int y, int n, int m) { int ans = 0; // Iterate the given matrix for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // Update the answer with // minimum moves required // for the given element // to reach the given index if (mat[i][j] == 1) { ans += abs(x - i) + abs(y - j); } } } // Return the number // of steps return ans; } // Driver Code int main() { // Given matrix vector<vector<int> > mat = { { 1, 0, 0, 0 }, { 0, 1, 0, 1 }, { 1, 0, 1, 1 } }; // Given position int x = 0, y = 2; // Function Call cout << findAns(mat, x, y, mat.size(), mat[0].size()) << endl; return 0; } |
Java
// Java program to calculate the // minimum steps required to reach // a given cell from all cells // consisting of 1's import java.util.*; class GFG{ // Function to calculate and // return the minimum number // of steps required to move // all 1s to (X, Y) static int findAns(int [][]mat, int x, int y, int n, int m) { int ans = 0; // Iterate the given matrix for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { // Update the answer with // minimum moves required // for the given element // to reach the given index if (mat[i][j] == 1) { ans += Math.abs(x - i) + Math.abs(y - j); } } } // Return the number // of steps return ans; } // Driver code public static void main(String[] args) { // Given matrix int [][]mat = { { 1, 0, 0, 0 }, { 0, 1, 0, 1 }, { 1, 0, 1, 1 } }; // Given position int x = 0, y = 2; // Function Call System.out.print(findAns(mat, x, y, mat.length, mat[0].length) + "\n"); } } // This code is contributed by amal kumar choubey |
Python3
# Python3 program to calculate # the minimum steps required to # reach a given cell from all # cells consisting of 1's # Function to calculate and # return the minimum number # of steps required to move # all 1s to (X, Y) def findAns(mat, x, y, n, m): ans = 0 # Iterate the given matrix for i in range(n): for j in range(m): # Update the answer with # minimum moves required # for the given element # to reach the given index if (mat[i][j] == 1): ans += (abs(x - i) + abs(y - j)) # Return the number # of steps return ans # Driver Code # Given matrix mat = [ [ 1, 0, 0, 0 ], [ 0, 1, 0, 1 ], [ 1, 0, 1, 1 ] ] # Given position x = 0y = 2 # Function call print(findAns(mat, x, y, len(mat), len(mat[0]))) # This code is contributed by shubhamsingh10 |
C#
// C# program to calculate the // minimum steps required to reach // a given cell from all cells // consisting of 1's using System; class GFG{ // Function to calculate and // return the minimum number // of steps required to move // all 1s to (X, Y) static int findAns(int [,]mat, int x, int y, int n, int m) { int ans = 0; // Iterate the given matrix for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { // Update the answer with // minimum moves required // for the given element // to reach the given index if (mat[i, j] == 1) { ans += Math.Abs(x - i) + Math.Abs(y - j); } } } // Return the number // of steps return ans; } // Driver code public static void Main(String[] args) { // Given matrix int [,]mat = { { 1, 0, 0, 0 }, { 0, 1, 0, 1 }, { 1, 0, 1, 1 } }; // Given position int x = 0, y = 2; // Function call Console.Write(findAns(mat, x, y, mat.GetLength(0), mat.GetLength(1)) + "\n"); } } // This code is contributed by Rajput-Ji |
15
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Recommended Posts:
- Minimize steps required to make all array elements same by adding 1, 2 or 5
- Minimize steps defined by a string required to reach the destination from a given source
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimize Steps required to obtain Sorted Order of an Array
- Minimum index i such that all the elements from index i to given index are equal
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Minimize operations required to make each element of Array equal to it's index value
- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- Find minimum steps required to reach the end of a matrix | Set - 1
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Find minimum steps required to reach the end of a matrix | Set 2
- Minimum steps required to reach the end of a matrix | Set 2
- Check if it is possible to move from (0, 0) to (x, y) in N steps
- Check if it is possible to move from (0, 0) to (X, Y) in exactly K steps
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Minimize count of given operations required to make two given strings permutations of each other
- Minimum number of steps required to obtain the given Array by the given operations
- Minimize subarray increments/decrements required to reduce all array elements to 0
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


