在二进制矩阵的中心得到 1 的最小步骤
给定一个N * N矩阵,其中N与所有 0 值都是奇数,除了单个单元格的值是 1。任务是找到最小可能的移动,以便在单次移动时将这个 1 移动到矩阵的中心,任何两个连续的行或两个连续的列都可以交换。
例子:
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
方法:
- 计算矩阵中心的位置为(cI, cJ) = (⌊N / 2⌋, ⌊N / 2⌋) 。
- 找到矩阵中1的位置并将其存储在(oneI, oneJ) 中。
- 现在,最小可能的移动将是abs(cI – oneI) + abs(cJ – oneJ) 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
Javascript
输出:
2
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。