Given an N * N matrix where N is odd with all 0 values except for a single cell which has the value 1. The task is to find the minimum possible moves to get this 1 to the center of the matrix when in a single move, any two consecutive rows or two consecutive columns can be swapped.
Examples:
Input: mat[][] = {
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}}
Output: 2
Operation 1: Swap the first and the second row.
Operation 2: Swap the second and the third row.Input: mat[][] = {
{0, 0, 0},
{0, 1, 0},
{0, 0, 0}}
Output: 0
Approach:
- Calculate the position of the center of the matrix as (cI, cJ) = (⌊N / 2⌋, ⌊N / 2⌋).
- Find the position of the 1 in the matrix and store it in (oneI, oneJ).
- Now, the minimum possible moves will be abs(cI – oneI) + abs(cJ – oneJ).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int N = 5; // Function to return the // minimum moves required int minMoves(int mat[N][N]) { // Center of the matrix int cI = N / 2, cJ = N / 2; // Find the position of the 1 int oneI = 0, oneJ = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (mat[i][j] == 1) { oneI = i; oneJ = j; break; } } } return (abs(cI - oneI) + abs(cJ - oneJ)); } // Driver code int main() { int mat[N][N] = { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 1, 0, 0 } }; cout << minMoves(mat); return 0; } |
Java
// Java implementation of the approach class GFG { static int N = 5; // Function to return the // minimum moves required static int minMoves(int mat[][]) { // Center of the matrix int cI = N / 2, cJ = N / 2; // Find the position of the 1 int oneI = 0, oneJ = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (mat[i][j] == 1) { oneI = i; oneJ = j; break; } } } return (Math.abs(cI - oneI) + Math.abs(cJ - oneJ)); } // Driver code public static void main(String[] args) { int mat[][] = { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 1, 0, 0 } }; System.out.print(minMoves(mat)); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of the approach N = 5 # Function to return the # minimum moves required def minMoves(mat): # Center of the matrix cI = N // 2 cJ = N // 2 # Find the position of the 1 oneI = 0 oneJ = 0 for i in range(N): for j in range(N): if (mat[i][j] == 1): oneI = i oneJ = j break return (abs(cI - oneI) + abs(cJ - oneJ)) # Driver code mat = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0]] print(minMoves(mat)) # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG { static int N = 5; // Function to return the // minimum moves required static int minMoves(int [,]mat) { // Center of the matrix int cI = N / 2, cJ = N / 2; // Find the position of the 1 int oneI = 0, oneJ = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (mat[i, j] == 1) { oneI = i; oneJ = j; break; } } } return (Math.Abs(cI - oneI) + Math.Abs(cJ - oneJ)); } // Driver code public static void Main(String[] args) { int [,]mat = {{ 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 1, 0, 0 }}; Console.Write(minMoves(mat)); } } // This code is contributed by 29AjayKumar |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Center element of matrix equals sums of half diagonals
- Farthest distance of a 0 from the center of a 2-D matrix
- Puzzle | Connect 9 circles each arranged at center of a Matrix using 3 straight lines
- Minimum steps to reach any of the boundary edges of a matrix | Set-2
- Find minimum steps required to reach the end of a matrix | Set - 1
- Minimum steps to reach any of the boundary edges of a matrix | Set 1
- 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
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
- Minimum steps to convert all top left to bottom right paths in Matrix as palindrome | Set 2
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- Minimize steps required to move all 1's in a matrix to a given index
- Count of ways to traverse a Matrix and return to origin in K steps
- Program to check if a matrix is Binary matrix or not
- Minimum operations required to set all elements of binary matrix
- Minimum flip required to make Binary Matrix symmetric
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.

