Minimum steps to get 1 at the center of a binary matrix

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:



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output:
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.





Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :