Minimum steps to get 1 at the center of a binary matrix
Last Updated :
10 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
const int N = 5;
int minMoves( int mat[N][N])
{
int cI = N / 2, cJ = N / 2;
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));
}
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
class GFG
{
static int N = 5 ;
static int minMoves( int mat[][])
{
int cI = N / 2 , cJ = N / 2 ;
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));
}
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));
}
}
|
Python3
N = 5
def minMoves(mat):
cI = N / / 2
cJ = N / / 2
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))
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))
|
C#
using System;
class GFG
{
static int N = 5;
static int minMoves( int [,]mat)
{
int cI = N / 2, cJ = N / 2;
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));
}
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));
}
}
|
Javascript
<script>
const N = 5;
function minMoves(mat)
{
let cI = parseInt(N / 2), cJ = parseInt(N / 2);
let oneI = 0, oneJ = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (mat[i][j] == 1) {
oneI = i;
oneJ = j;
break ;
}
}
}
return (Math.abs(cI - oneI) + Math.abs(cJ - oneJ));
}
let 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 ] ];
document.write(minMoves(mat));
</script>
|
Time Complexity: O(N^2)
Auxiliary Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...